我有一个UIViewController作为一种菜单。该菜单由多个盒/瓦片组成,这些盒子/瓦片是包含带有描述的UILabel的UIViews,链接到显示更多信息的另一个UIViewController的UIButton和链接到显示该应用的另一个区域的UIViewController的另一个UIButton。如何处理动态创建的UIButtons
目前有3个这样的盒子,但这个会增长。目前,我已经硬编码了每一个这样的:
- (void)drawProceduresBox
{
self.viewProceduresBox = [[UIView alloc] initWithFrame:CGRectMake((_screenWidth * 0.05), (_screenHeight * 0.35), (_screenWidth * 0.9), (_screenHeight * 0.25))];
[_viewProceduresBox setBackgroundColor:[UIColor whiteColor]];
_viewProceduresBox.layer.borderWidth = 1.0f;
_viewProceduresBox.layer.borderColor = [UIColor grayColor].CGColor;
CGFloat boxHeight = _viewProceduresBox.frame.size.height;
CGFloat boxWidth = _viewProceduresBox.frame.size.width;
self.buttonShowProcedures = [[UIButton alloc] init];
[_buttonShowProcedures setFrame:CGRectMake((boxWidth * 0.8), (boxHeight * 0.4), (boxWidth * 0.1), (boxHeight * 0.2))];
[_buttonShowProcedures setBackgroundColor:[UIColor greenColor]];
[_buttonShowProcedures addTarget:self action:@selector(showProcedures) forControlEvents:UIControlEventTouchUpInside];
_buttonshowMoreInfo = [[appButton alloc] init];
[_buttonshowMoreInfo setFrame:CGRectMake((boxWidth * 0.6), (boxHeight * 0.6), (boxWidth * 0.1), (boxHeight * 0.2))];
[_buttonshowMoreInfo setBackgroundColor:[UIColor blueColor]];
[_buttonshowMoreInfo setWhatToShow:@"vetMed"];
[_buttonshowMoreInfo addTarget:self action:@selector(showMoreInfo:) forControlEvents:UIControlEventTouchUpInside];
[_viewProceduresBox addSubview:_buttonShowProcedures];
}
有可能是这些框10+,所以我就想办法来动态创建。这是我到目前为止已经试过:
-(void)drawBox:(NSString*)moduleName boxFrame:(CGRect)boxFrame labelDesc:(NSString*)labelDesc
{
UIView *moduleInfoBox = [[UIView alloc] initWithFrame:boxFrame];
[moduleInfoBox setBackgroundColor:[UIColor purpleColor]];
CGFloat boxHeight = moduleInfoBox.frame.size.height;
CGFloat boxWidth = moduleInfoBox.frame.size.width;
UILabel *moduleLable = [[UILabel alloc] init];
[moduleLable setFrame:CGRectMake((boxWidth * 0.05), (boxHeight * 0.05), (boxWidth * 0.6), (boxHeight * 0.9))];
[moduleLable setBackgroundColor:[UIColor yellowColor]];
[moduleLable setText:labelDesc];
UIButton *showModuleButton = [[UIButton alloc] init];
[showModuleButton setFrame:CGRectMake((boxWidth * 0.8), (boxHeight * 0.15), (boxWidth * 0.1), (boxWidth * 0.2))];
[showModuleButton setBackgroundColor:[UIColor grayColor]];
[showModuleButton addTarget:self action:@selector(showModule:) forControlEvents:UIControlEventTouchUpInside];
UIButton *showMoreInfoButton = [[UIButton alloc] init];
[showMoreInfoButton setFrame:CGRectMake((boxWidth * 0.8), (boxHeight * 0.65), (boxWidth * 0.1), (boxWidth * 0.1))];
[showMoreInfoButton setBackgroundColor:[UIColor orangeColor]];
[showMoreInfoButton addTarget:self action:@selector(showMoreInfo:) forControlEvents:UIControlEventTouchUpInside];
[moduleInfoBox addSubview:moduleLable];
[moduleInfoBox addSubview:showModuleButton];
[moduleInfoBox addSubview:showMoreInfoButton];
_scrollHeight = _scrollHeight + (moduleInfoBox.frame.origin.y + moduleInfoBox.frame.size.height);
[_scrollView addSubview:moduleInfoBox];
}
,我会叫这样的方法(在这种情况下,三盒):
CGRect formularyFrame = CGRectMake((_screenWidth * 0.05), (_screenHeight * 0.05), (_screenWidth * 0.9), (_screenHeight * 0.25));
CGRect proceduresFrame = CGRectMake((_screenWidth * 0.05), (_screenHeight * 0.35), (_screenWidth * 0.9), (_screenHeight * 0.25));
CGRect vetMedFrame = CGRectMake((_screenWidth * 0.05), (_screenHeight * 0.65), (_screenWidth * 0.9), (_screenHeight * 0.25));
[self drawBox:@"Formulary" boxFrame:formularyFrame labelDesc:@"Desc about formulary module"];
[self drawBox:@"Procedures" boxFrame:proceduresFrame labelDesc:@"Desc about procedures module"];
[self drawBox:@"VetMed" boxFrame:vetMedFrame labelDesc:@"Desc about vetMed module"];
我遇到的问题是动态生成的在这种情况下,UIButtons需要链接到相关的东西,UIViewControllers。每个按钮都有一个选择器,用于触发方法'showModule'和'showModuleInfo'。
在这些方法中,我该如何判断UIButton是否被按下,因此知道将哪个UIViewController推送到navigationController?基本上,我想沿着线的东西:
-(void)showModule:(NSString*)moduleToShow
{
[self.navigationController pushViewController:moduleToShow animated:NO];
}
但据我所知,你可以不加参数“@选择”分配:
[_buttonshowMoreInfo addTarget:self action:@selector(showMoreInfo:) forControlEvents:UIControlEventTouchUpInside];
我该怎么办呢?
好,动态创建。如果你想要通过一个循环。当你不知道有多少人时,把他们放在一个数组中。或者使用表格并将按钮添加到单元格。或者根本不要在任何局部变量中保留它们,而是给它们分配一个标签。您可以使用该标签,一旦按钮被点击以确定哪个按钮被点击 - 如果您需要知道。 –
这就是我想要最终实现的目标。我将有一个循环遍历存储在SQLite数据库中的模块名称数组,然后使用相关链接构建每个框以动态创建UIViewControllers。我只是不知道如何将动态创建的UIButtons与加载动态创建的UIVC的方法绑定在一起。 – Ryan
不知何故,你将不得不知道哪个视图控制器将被调用每个按钮。如果你可以使用一个整数作为id,让我们说一个数组的索引。然后使用for循环来创建带'i'作为循环变量的视图。将'i'分配给每个按钮的'tag'属性。当按钮被按下时,一个动作被调用,'sender'被移交给它。发送者是一个'UIView'子类,它是被按下的那个按钮。获取它的'标记'值并从那里开始... –