Search code examples
objective-cloopsoutlet

Loop across all outlets


I want to do a loop across every nine outlets (UIButton's, called btn1, btn2, btn3... btn9) that I have, like:

for(int i = 0; i < 9; i++) {
    [[btn(%@), i] setImage:someOne forState:UIControlStateNormal]; // I know that this is ridiculous, but it's just a way to demonstrate what I'm saying. :-)
}

Any tip?

Thanks a lot!


Solution

  • Have all the outlets you want to loop to loop through on a separate view.

    for(int subviewIter=0;subviewIter<[view.subviews count];subviewIter++)
    {
        UIbutton *button = (UIbutton*)[view.subviews objectAtIndex:subviewIter];
        // Do something with button.
    }