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 requ
Here's what I did:
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!
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];
}
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)