Search code examples
iosobjective-cuibuttonuistoryboardiboutlet

I have IBOutlets for my game view controller UIButtons that need to be organized better


I have IBOutlets for my UIButtons like what is shown below. This is continued 30 times. I am wondering if there is a way to make it where I do not have to list each button outlet like this and make it more organized?

If anyone can point me in the right direction I will deeply appreciate it.

@property (weak,nonatomic) IBOutlet UIButton *level1Button;
@property (weak,nonatomic) IBOutlet UIButton *level2Button;
@property (weak,nonatomic) IBOutlet UIButton *level3Button;

Solution

  • Instead of lots of outlets, you can use an outlet collection. In your .h file:

    @property (weak, nonatomic) IBOutletCollection(UIButton) NSArray *buttons;
    

    Then, you can control-drag your buttons to that line and they will all be in the array, in the order that you dragged them.