Add a multiple buttons to a view programmatically, call the same method, determine which button it was
我想以编程方式将多个uibuttons添加到一个视图中-按钮的数量在编译时未知。
我可以这样做一个或多个uibutton(在一个循环中,但为了简单起见简称为):
1 2 3 4 5 6 7 | UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown]; [button setTitle:@"Button x" forState:UIControlStateNormal]; button.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); [view addSubview:button]; |
从此链接复制/编辑:如何以编程方式创建基本的uibutton?
但如何确定buttonchecked中的哪个按钮被单击?如果可能的话,我想传递标签数据来识别按钮。
您可以将对实际按钮对象的引用保留在重要的地方(如数组),或者将按钮的标记设置为有用的东西(如其他数据数组中的偏移量)。例如(使用标记,因为这通常是有用的):
1 2 3 4 5 6 7 8 9 10 11 12 13 | for( int i = 0; i < 5; i++ ) { UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [aButton setTag:i]; [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; [aView addSubview:aButton]; } // then ... - (void)buttonClicked:(UIButton*)button { NSLog(@"Button %ld clicked.", (long int)[button tag]); } |
您可以为按钮分配一个标记。
1 | button.tag = i; |
然后在
1 2 3 4 | -(void)buttonClicked:(UIButton*)sender { int tag = sender.tag; ... } |
我认为这是最好的答案:http://conecode.com/news/2012/05/ios-how-to-create-a-grid-of-ui按钮/
如果您想在运行时添加按钮,那么将有10个20个50或更多的按钮。然后您应该在这种情况下使用UI滚动视图。
当按钮生成时,您的滚动视图大小应该相应地增加。
你可以这样写代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | scrollview = [[UIScrollView alloc] init]; scrollview.contentSize = CGSizeMake(INVOICE_ADDITEM_SCROLLVIEW_CONTENT_WIDTH, INVOICE_ADDITEM_SCROLLVIEW_CONTENT_HEIGHT); scrollview.frame = CGRectMake(0,50, SCROLLVIEW_WIDTH, SCROLLVIEW_HEIGHT); // scrollview.backgroundColor = [UIColor whiteColor]; scrollview.scrollsToTop = NO; scrollview.delegate = self; [self.view addSubview:scrollview]; for (int pos = 0; pos < 2; pos++) { UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom]; [but setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal]; [but setImage:[UIImage imageNamed:@"checkbox_active.png"] forState:UIControlStateSelected]; [but setFrame:CGRectMake(TXT_FLD_X_CORD+90, 295, 20, 20)]; // [but setCenter:CGPointMake( 50, i*40+20 )]; but.tag = pos; /*if(pos==0) { [but setImage:[UIImage imageNamed:@"checkbox_active.png"] forState:UIControlStateNormal]; // [but setImage:[UIImage imageNamed:@"checkbox_active.png"] forState:UIControlStateSelected]; }*/ [but setCenter:CGPointMake(pos*90+125 ,310)]; [but addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside]; [scrollview addSubview:but]; } |
为每个按钮提供不同的标签,请使用以下代码:
1 2 | [btn1 addTarget:self action:@selector(pressbtn:) forControlEvents:UIControlEventTouchUpInside]; [btn2 addTarget:self action:@selector(pressbtn:) forControlEvents:UIControlEventTouchUpInside]; |
方法
1 2 3 4 5 6 7 8 9 10 | -(void)pressbtn:(id)sender { UIButton *button=sender; if (button.tag==1) { NSLog(@"Press button 1"); } if (button.tag==2) { NSLog(@"Press button 2"); } |
等等,检查调用哪个按钮
Swift版本(带标签):
1 2 3 4 5 6 | for index in 1...5 { var label = UILabel() label.tag = index labelsDictionary["Label\(index)"] = label self.addSubview(label) } |
使用self.viewwithtag(i)作为uilabel调用:
1 | (cell.viewWithTag(5) as UILabel).text |
有人可能有这个问题:
JasonCoco的回答对我来说很有效,直到我想使用标签从一个被定义为属性的NSarray访问属性。
事实证明,房地产必须定义为"保留"而不是"弱"。
对于每个按钮,设置一个适当的标记,然后在操作中引用该标记。即
1 2 3 4 | UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; ... button.tag = 1 [view addSubview:button]; |
如果要在循环中创建按钮,可以根据迭代的索引轻松地设置标记。然后在你的行动中:
1 2 3 4 5 | - (void)aButtonWasTapped:(UIButton *)source { if( source.tag == 1 ) { // etc } } |
uibutton具有