Search code examples
iosobjective-cxcodeios7

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?


Solution

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