How do I edit all UIButtons with a specific tag in objective-c?
I want to edit all buttons with a specific tag. I have tagged each of the UIButtons (which I built in IB) from 0-15. I then used the tag button as the index values when searching in my NSMutableArray (investigationsArray). There are 16 items in my array.
I want to implement something like this in my viewWillAppear:
if (theButtonTag == 0){
[button setTitle: [[investigationsArray objectAtIndex:0]objectForKey:@"name"] forState:UIControlStateNormal];
}
I would like to simplify my code so eventually I can use a f开发者_开发技巧or statement like this:
for (buttonTag = 0; buttonTag < [investigationsArray count]; buttonTag ++){
if (theButtonTag == i){
[button setTitle: [[investigationsArray objectAtIndex:i]objectForKey:@"name"] forState:UIControlStateNormal];
}
}
I've looked all over google and can't find anything. Thanks guys.
Thanks to Inspire48 and Alex Nichol I managed to come up with an answer for this. If you try to start at i = 0
, the code throws an error:'NSInvalidArgumentException', reason: '-[UIView setTitle:forState:]: unrecognized selector sent to instance 0xa180970
. So to make up for this, I just added a blank entry at index0 into my pList, which allows me to start my for()
statement at i = 1
instead of i = 0
.
for (int i = 1; i < [investigationsArray count]; i++) {
UIButton * button = (UIButton *)[self.view viewWithTag:i];
NSString * title = [[investigationsArray objectAtIndex:i] objectForKey:@"name"];
[button setTitle:title forState:UIControlStateNormal];
}
The other change I had to make on the code that both Inspire48 and Alex Nichol used is the following. Instead of using [self viewWithTag:i]
you need to use [self.view viewWithTag:i]
.
Thanks again guys!
Use a standard for
loop, as you have up there. UIView
has a method called viewWithTag, which will return a view with the tag specified. It's exactly what you want.
Code snippet:
for (int i = 0; i <= 15; i++) {
UIButton *button = (UIButton *)[self viewWithTag:i];
[button setTitle: [[investigationsArray objectAtIndex:0]objectForKey:@"name"] forState:UIControlStateNormal];
}
Inspire48's code above is almost correct, with a small bug. You want to loop through all of the tags from 0 to 15, and set the title of that button to the value in your array. Here is some example code:
for (int i = 0; i < [investigationsArray count]; i++) {
UIButton * button = (UIButton *)[self viewWithTag:i];
NSString * title = [[investigationsArray objectAtIndex:i] objectForKey:@"name"];
[button setTitle:title forState:UIControlStateNormal];
}
I would suggest in the end NOT using tags of buttons, but rather having the buttons as keys in an NSDictionary. You could then have something like:
for (UIView * view in [self subviews]) {
if ([view isKindOfClass:[UIButton class]) {
UIButton * button = (UIButton *)view;
NSString * title = [[investigationsDictionary objectForKey:button] objectForKey:@"name"];
[button setTitle:title forState:UIControlStateNormal];
}
}
You can initialise your dictionary as follows:
NSDictionary * investigationsDictionary;
...
investigationsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:button1, myValue,...,nil];
精彩评论