Search code examples
iosuibuttonnsmutablearrayiboutletcollection

Retrieving tag from IBOutletCollection of UIButtons


I have an IBOutletCollection of UIButtons. What is the most efficient way of retrieving the tags of any index in my mutableArray of buttons.

@property(retain) IBOutletCollection(UIButton) NSMutableArray *emptySpaces;

This is how my buttons are declared

@property (strong, nonatomic) IBOutlet UIButton *position1;

Ive tried the following. What is it I'm doing wrong? Thank you

if(emptySpaces[0].tag == 1){

}

Or

 if([emptySpaces objectAtIndex:0].tag == 1){

  }

Solution

  • To answer your initial question, an id which is what NSMutableArray -objectAtIndex: returns, doesn't have a receiver named tag. You should first cast your result to a UIButton before sending the tag message. Like this: ((UIButton *)self.emptySpaces[0]).tag

    You could try something like this:

    for (UIButton *button in self.emptySpaces) {
        if (button.tag == 1) {
    
        }
    }