Reputation: 1924
I added 10 buttons to a view (example view name is "menuView"), now I want to remove the background image for 2nd, 3rd, 4th buttons. I wrote the code like this
for(id btn in [menuView subViews]){
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
The problem with this code is, it is removing all 10 button's backGroundimage, but I need to set nil
for the 2nd, 3rd, and 4th buttons
Upvotes: 5
Views: 12573
Reputation: 341
Firstly, are you placing the buttons using Interface Builder?
If so, I'd recommend placing numbered tags for each of the buttons and then you can use something like the following to find the appropriate buttons and remove the background image.
for(UIButton *buttonname in [yourView subViews]){
if (buttonname.tag == 2 || buttonname.tag == 3 || buttonname.tag == 4) {
[buttonname setBackgroundImage:nil forState:UIControlStateNormal];
}
}
If you're creating them programmatically and sequentially, I'd recommend placing the buttons in an array as they're made and just remove the background of the buttons using "objectAtIndex".
Upvotes: 0
Reputation: 1736
Just to expand on my comment.
Using an IBOutletCollection which you can point an array to many objects in the nib. You declare this as such (synthesizing in the implementation):
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *threeButtons;
This says to IB that it's a collection of UIButton elements. In IB, you connect this to the three buttons you wish to remove the background image for, by control dragging from it to the buttons. Once this is done the array will contain those buttons you connected up and you can loop like so:
for (UIButton *button in self.threeButtons) {
[button setBackgroundImage:nil forState:UIControlStateNormal];
}
Again, the link to a more detailed explanation can be found at: http://bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection
Upvotes: 2
Reputation: 150615
If you create a tag for the buttons that you add, you can filter against them.
for(UIButton *btn in [menuView subViews]){
if (btn.tag == 2 || btn.tag == 3 || btn.tag == 4) {
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
}
Of course, you need to make sure that there are no other views in menuView that could share the same tag. So the choices are to make the tags large, unique values, or checking that they are actually UIButtons. I've edited this assuming that the only subviews of menuView are UIButtons. Enumerating over UIButtons won't cause compiler warnings about tag
not being a property of NSObject.
UIButton
is a subclass of UIControl
which is a subclass of UIView
. UIView
has the tag
property, so UIButton
inherits this property. It's useful to look at the docs for a class that you are using, and continue up the hierarchy to see if there are properties or methods that are useful for what you need to do.
Upvotes: 11
Reputation:
You could have assigned tags to the buttons from 1 to 10 when adding them to menu view. And now with the help of tags, we can decided what to do with buttons.
Upvotes: 1
Reputation: 8991
When creating the buttons, try using the "tag" property. Then, when you're setting the background to nil, you could check for btn.tag == 2,btn.tag == 3 or btn.tag == 4.
Upvotes: 1