问题
I have 3 buttons at the bottom of my view controller, btn1 btn2 btn3, I'm using them instead of the tab bar as it is not possible to entirely customize a tab bar as per my requirements.
Now the issue is, when btn1 is pressed, I want it to change its image, to a grey rectangle, instead of the normal state image. I have set the images for both states in the outlet I declared for the button, btn1Outlet, using the setimage and uicontrolstate properties of the outlet.
The problem is, I can't keep the button selected until say either btn2 or btn3 is pressed. btn one changes to the selected state image only as long as it is pressed, the moment I leave it, it changes back to its normal state.
How can I keep btn1's image as selected image until any of the other 2 buttons are pressed?
回答1:
What you did is set an image for the "Highlighted" state, this is why when you push it you can see your image.
What you want to do is
1) set the image for the SELECTED state
2) create a property for your view controller (just controll drag the button to the header) using the assistant view (while on storyboard, second square on the top right)
3) on your method for the button's action type:
button.selected = !button.selected;
(obviously replace button to whatever you named your property to)
回答2:
Here's what I did:
- Link all 3 buttons to the following action method
- Create array of all 3 buttons
- Set the button that invoked the method to selected
Set the other 2 buttons to not selected
- (IBAction)buttonPressed:(id)sender { NSArray* buttons = [NSArray arrayWithObjects:btn1, btn2, btn3, nil]; for (UIButton* button in buttons) { if (button == sender) { button.selected = YES; } else { button.selected = NO; } } }
Hope this helps.
Cheers!
回答3:
To keep the button selected, you need to call setSelected:YES in the method that your button calls. For example:
- (void) methodThatYourButtonCalls: (id) sender {
[self performSelector:@selector(flipButton:) withObject:sender afterDelay:0.0];
}
- (void) flipButton:(UIButton*) button {
if(button.selected)
[button setSelected:NO];
else
[button setSelected:YES];
}
I know it looks a little weird calling performSelector: instead of just calling [sender setSelected:YES], but the latter never works for me, while the former does!
In order to get the buttons to deselect when a different one is pressed, I suggest adding an instance variable that holds a pointer to the currently selected button, so when a new one is touched you can call flipButton: to deselect the old one accordingly. So now your code should read:
add a pointer to your interface
@interface YourViewController : UIViewController
{
UIButton *currentlySelectedButton;
}
and these methods to your implementation
- (void) methodThatYourButtonCalls: (id) sender {
UIButton *touchedButton = (UIButton*) sender;
//select the touched button
[self performSelector:@selector(flipButton:) withObject:sender afterDelay:0.0];
if(currentlySelectedButton != nil) { //check to see if a button is selected...
[self flipButton:currentlySelectedButton];
currentlySelectedButton = touchedButton;
}
- (void) flipButton:(UIButton*) button {
if(button.selected)
[button setSelected:NO];
else
[button setSelected:YES];
}
来源:https://stackoverflow.com/questions/11181340/keeping-a-uibutton-pressedstate-selected-highlighted-until-another-button-is-p