ggf85
ggf85

Reputation: 49

Add button outlets to IBoutletCollection Xcode

I want to add 15 UIButtons to a IBOutletCollection and change the label of each UIButton separately. Can I assign a tag to each button and then somehow change the button label relating to the tag of the button? Or do they need to be individual outlets for me to change the individual button label?

Upvotes: 2

Views: 523

Answers (3)

Yogendra
Yogendra

Reputation: 1716

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

Set all buttons with unique tags. Make one IBAction method and connect it to all your buttons in IB. And Use switch like this (let tags are 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;
   }

Upvotes: 0

Mohit
Mohit

Reputation: 3416

Write this code in button click method. set tag and check condition for that and set title for particular tag

-(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];
}

Upvotes: 0

Bernhard Grabowski
Bernhard Grabowski

Reputation: 469

You can fish the buttons according to their tags out of the IBOutletCollection Array in a for loop

UIButton *theButton;
for (theButton in yourIBOutletCollectionArray){

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

}

Upvotes: 1

Related Questions