Reputation: 1473
I have difficulties with make an array of UI Buttons rather than having many separate ones. I'm trying to create four and have:
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UIButton *b0;
IBOutlet UIButton *b1;
IBOutlet UIButton *b2;
IBOutlet UIButton *b3;
}
@property (nonatomic, strong) UIButton *b0;
@property (nonatomic, strong) UIButton *b1;
@property (nonatomic, strong) UIButton *b2;
@property (nonatomic, strong) UIButton *b3;
@end
ViewController.m:
- (IBAction)b0Click:(id)sender {
//Do something
}
- (IBAction)b1Click:(id)sender {
//Do something
}
- (IBAction)b2Click:(id)sender {
//Do something
}
- (IBAction)b3Click:(id)sender {
//Do something
}
Any help would be appreciated. Thanks!
Upvotes: 0
Views: 419
Reputation: 57060
Define a IBOutletCollection
:
@property (nonatomic, strong) IBOutletCollection(UIButton) buttons;
Now connect all your buttons to this collection.
You can now accessing the buttons by:
[self.buttons enumerateObjectsUsingBlock:^ (UIButton* button, NSUInteger index, BOOL* stop) {
//Do stuff here
}];
I must warn you against making assumptions about the order of the buttons inside the array. It is better to give each button a tag, and then in the action method decide which code path to take according to their tag.
Upvotes: 1