keeping a UIButton pressed(state selected/highlighted),until another button is pressed?

前端 未结 3 1368
庸人自扰
庸人自扰 2021-02-09 19:26

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

相关标签:
3条回答
  • Here's what I did:

    1. Link all 3 buttons to the following action method
    2. Create array of all 3 buttons
    3. Set the button that invoked the method to selected
    4. 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!

    0 讨论(0)
  • 2021-02-09 19:51

    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];
    
    }
    
    0 讨论(0)
  • 2021-02-09 19:54

    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)

    0 讨论(0)
提交回复
热议问题