Objective-C: UIButtons Array

If I have a large number of buttons installed in Interface Builder, how can I put them into an array through code?

For example, if I have three buttons and in the interface file I define them:

@property (nonatomic, retain) IBOutlet UIButton *button1; @property (nonatomic, retain) IBOutlet UIButton *button2; @property (nonatomic, retain) IBOutlet UIButton *button3; 

If I want to rename each of these buttons to say β€œHello”, I want to be able to split them into an array, and then use a function such as:

 for (int i = 0; i < someArray.count; i++) { someArray[i].text = @"Hello"; } 

Can someone provide information on how this is achieved (if possible)?

thanks

+7
source share
5 answers

Assuming the buttons in IB are correctly connected to the IBOutlet properties, it is just as simple:

 NSArray *buttonArray = [[NSArray alloc] initWithObjects:button1,button2,button3,nil]; 
+5
source

Another approach is to connect buttons to NSArray in Interface Builder.

 @property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons; 
+12
source

In the following code, 5 buttons will be added, each of which will have a new tag, and also connect it to the event. Buttons will be placed next to 5px indentation:

If you plan to access a button outside the for{} below, you can define your button in your header, otherwise you can define it inside the area. UIButton * settButton;

.m

 CGFloat staticX = 5; // Static X for all buttons. CGFloat staticWidth = 60; // Static Width for all Buttons. CGFloat staticHeight = 56; // Static Height for all buttons. CGFloat staticPadding = 5; // Padding to add between each button. for (int i = 0; i < 5; i++) { settButton = [UIButton buttonWithType:UIButtonTypeCustom]; [settButton setTag:i]; [settButton setFrame:CGRectMake((staticX + (i * (staticHeight + staticPadding))),5,staticWidth,staticHeight)]; [settButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.png",i]] forState:UIControlStateNormal]; [settButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchDown]; [settButtonView addSubview: settButton]; } [self.view addSubview: settButtonView]; 
+7
source

Since you created the buttons directly in Interface Builder, can't you just set them directly in the code?

A more general solution would be something like this ... if I assume two things: they are all sub-views of the UIViewController and that these UIButtons are the only UIButtons in this view, you can do something like this:

 for (UIView *aView in [self.view subviews]) { if ([aView isKindOfClass:[UIButton class]) { UIButton *aButton = (UIButton *) aButton; [aButton setTitle:@"Hello" forState:UIControlStateNormal]; } } 
+2
source

I think the easiest way is to create one button that you enable and add to the mutable array:

in * .m:

 UIButton *buttonChar; 

in your function:

 buttonArray = [[NSMutableArray alloc] init]; for (int i=0; i<lenghtWord; i++) { buttonChar = [[UIButton alloc] initWithFrame:CGRectMake(.2*size.height+(float)i*charFontSize, .5*size.width, charFontSize, charFontSize)]; buttonChar.tag = i; [buttonChar setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)]; [buttonChar setTitle:[[NSString alloc] formatWithString:@"Button %d",i] forState:UIControlStateNormal]; buttonChar.titleLabel.font = [UIFont boldSystemFontOfSize:15]; buttonChar.titleLabel.textAlignment = UITextAlignmentCenter; buttonChar.layer.cornerRadius = 5; buttonChar.layer.masksToBounds = YES; buttonChar.layer.borderWidth = 1; buttonChar.layer.borderColor = [[UIColor blackColor] CGColor]; [buttonChar setBackgroundColor:[UIColor whiteColor]]; [buttonChar addTarget:self action:@selector(buttonAction:withEvent:) forControlEvents:UIControlEventTouchDragInside]; [buttonArray addObject:buttonChar]; } 
Buttons

available through:

 [[buttonArray objectAtIndex:j] setTitle:@" " forState:UIControlStateNormal]; 

Since the tag option is also filled, I can always tell which button from the array was affected if it was displayed (buttonAction: withEvent :)

Greetings

0
source

All Articles