添加按钮出口到IBoutletCollection Xcode

时间:2014-08-01 07:18:12

标签: ios objective-c xcode ios7

我想将UIButtons添加到IBOutletCollection并分别更改每个UIButton的标签。我可以为每个按钮分配标签,然后以某种方式更改与按钮标签相关的按钮标签吗?或者他们是否需要成为个人出口才能更改个别按钮标签?

3 个答案:

答案 0 :(得分:1)

您可以在for循环中根据IBOutletCollection数组中的标签捕捉按钮

UIButton *theButton;
for (theButton in yourIBOutletCollectionArray){

    if (theButton.tag == /* Your Tag Number OF Choice */) {
    [theButton setTitle:(NSString *) forState:(UIControlState)];
    }

}

答案 1 :(得分:1)

在按钮点击方法中编写此代码。设置标记并检查条件并设置 特定标记

标题
-(IBAction)btnClick:(id)sender{

UIButton * btn = (UIButton *)sender;
int btag = btn.tag;

if(btag == 1)
    [btn setTitle:@"Your Title " forState:UIControlStateNormal];
else if (btag == 2)
     [btn setTitle:@"Your Title " forState:UIControlStateNormal];
}

答案 2 :(得分:0)

Avoid 0 as tag value, because 0 is default tag value for all controls.

使用唯一标记设置所有按钮。制作一个IBAction方法并将其连接到IB中的所有按钮。并像这样使用switch(让标签为3,4,5 ......) -

   -(IBAction) btnClick:(id)sender
    {
       UIButton * btn = (UIButton *)sender;
       switch([btn tag])
       {
          case 3:
             [btn setTitle:@"First_Button_Title " forState:UIControlStateNormal]; 
             break; 
          case 4:
            // change btn title 
             break; 
          case 5:
            // change btn title 
             break; 
          default:
              break;
   }