How to set the UIButton state to be highlighted after pressing it

65,990

Solution 1

[sender setSelected:YES]; 

or you can simulate this effect with two image for your UIButton (notselectedimage.png and selectedimage.png), then keep track button state with a BOOL variable like BOOL buttonCurrentStatus;. Then in .h file:

BOOL buttonCurrentStatus;

and in .m file

// connect this method with Touchupinside function
- (IBAction)changeState:(UIButton*)sender
{
    /* if we have multiple buttons, then we can
       differentiate them by tag value of button.*/
    // But note that you have to set the tag value before use this method.

  if([sender tag] == yourButtontag){

    if (buttonCurrentStatus == NO)
    {
        buttonCurrentStatus = YES;
        [butt setImage: [UIImage imageNamed:@"selectedImage.png"] forState:UIControlStateNormal];
        //[self performSomeAction:sender];
    }
    else
    {
        buttonCurrentStatus = NO;
        [butt setImage:[UIImage imageNamed:@"notSelectedImage.png"] forState:UIControlStateNormal];
        //[self performSomeAction:sender];
    }   
  }
}

Solution 2

- (void)mybutton:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = ![button isSelected]; // Important line
    if (button.selected)
    {
        NSLog(@"Selected");
        NSLog(@"%i",button.tag);
    }
    else
    {
        NSLog(@"Un Selected");
        NSLog(@"%i",button.tag);

    }
 }

Solution 3

The highlighted state is used to highlight the button while it is being touched. A touch down event in the button highlights it. You should use the "selected" state instead.

If what you want to do is perform an action after the button is pressed, don't attach your method to the state change event, attach your method to the TouchUpInside event.

Solution 4

I just find a way, so I share it, just in case...

I kept my UIButton and set one image for each state (so you could go up to a 4 states button). I set the UserInteractionEnabled to NO -> This button won't receive any touch. The purpose of this first button is to show a state

I create a second custom UIButton with the same frame than the first one. For this one, none image will be set for the state (it's a fully transparent button). The purpose of this button is to catch the touch event. So I added a target to this button on the TouchUpInside event. And then when the event is fired, I change the state of the first button to Disabled, Highlighted, Selected, or none of these state (= Default state).

Everything is working like a charm!

Solution 5

The way you describe it, you'd be better off subclassing UIView to create your own three-state button.

Actually, you should even implement your own multistate buttonView, and manage the state it's in internally via an array of PNG for the looks and an array of states to know how many times it's been pressed.

Share:
65,990
A for Alpha
Author by

A for Alpha

iOS application developer and a huge steve Jobs fan. The beauty of the apple products motivated me to be a iOS apps developer and i am in love with it... :)

Updated on April 13, 2020

Comments

  • A for Alpha
    A for Alpha about 4 years

    I have a typical requirement wherein I need to keep a button in highlighted state after pressing it. I need to perform a task which should work only when a button is in highlighted state. Actually I am setting a button state to highlighted programatically.

    [sender setHighlighted:YES];

    And once the button is in highlighted state i need to perform another action.

    - (IBAction)changeState: (UIButton*)sender
    {   
        if (sender.highlighted == YES)
        {
            [self performSomeAtion:sender];
        }
    }
    

    But, to my horror, whenever I press any button, the above condition is becoming true and the action is being performed repeatedly. Is there any way in which i can keep a UIButton's state to be highlighted after pressing it?

    EDIT - Actually I need to perform 3 different actions for 3 different states of the button. I am already making use of selected state and normal state. Now, I need to make use of the highlighted state.

  • A for Alpha
    A for Alpha over 12 years
    like it mentioned in my question, i need to use highlighted state of the button not the selected state.
  • A for Alpha
    A for Alpha over 12 years
    +1 for nice explanation. Actually i need to perform 3 different actions for 3 different states of the button. This is where i got stuck up.. Any help would be greatly appreciated(Please check my updated question)
  • Narayana
    Narayana over 12 years
    UIButton *btn_tmp=sender;if(!(btn_temp.selected)){//it true when button not selected ever }
  • A for Alpha
    A for Alpha over 12 years
    Thanks for the answer vijay.. Your answer works fine if i have a single button. But my case is a bit more complicated. I have multiple buttons which should perform 3 actions for 3 states.. Any suggestions??? +1 for ur answer too...
  • Vijay-Apple-Dev.blogspot.com
    Vijay-Apple-Dev.blogspot.com over 12 years
    UISegmentControl u know.use that.if u want more help then reply.mobisoftinfotech.com/blog/iphone/…
  • A for Alpha
    A for Alpha over 12 years
    i have set the button to highlighted state. But, when i try to check the status of the button i am unable to get the exact state.i guess when i press the same button, the state is getting changed from highlighted to something else.. is there any way to find that narayana?.
  • Scott Lieberman
    Scott Lieberman over 11 years
    If you have multiple buttons, all you have to do is take Vijay's answer and tweak it by addings tags to your buttons and then doing... UIButton *button = (UIButton*)sender; if (button.tag == 1) ...