WolfpackEE
WolfpackEE

Reputation: 91

Unable to keep UIButton in selected state after TouchUpInside Event

I have the need for an UIButton to maintain a pressed state. Basically, if a button is in a normal state, I want to touch the button, it highlight to its standard blue color and then stay blue after lifting my finger. I made the following UIAction and connected the buttons Touch Up Inside event to it.

-(IBAction) emergencyButtonPress:(id) sender
{
    if(emergencyButton.highlighted)
    {
        emergencyButton.selected = NO;
        emergencyButton.highlighted = NO;
    }
    else
    {
        emergencyButton.selected = YES;
        emergencyButton.highlighted = YES;
    }
}

But what happens, after I remove my finger, the button goes back to a white background. For a test I added a UISwitch and have it execute the same code:

-(IBAction) emergencySwitchClick:(id) sender
{
    if(emergencyButton.highlighted)
    {
        emergencyButton.selected = NO;
        emergencyButton.highlighted = NO;
    }
    else
    {
        emergencyButton.selected = YES;
        emergencyButton.highlighted = YES;
    }
}

In this case the button toggles to a highlighted and non-highlighted state as I would expect.

Is there another event happening after the Touch Up Inside event that is resetting the state back to 'normal'? How do I maintain the highlighted state?

Upvotes: 6

Views: 8740

Answers (7)

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Try this simple answer....

- (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);

    }
 }

Upvotes: 0

circuitlego
circuitlego

Reputation: 3479

This worked for me:

- (void)tapButton:(id)sender{
    UIButton *button = sender;
    if (!button.selected){
        [self performSelector:@selector(highlight:) withObject:button afterDelay:0.0];
    }else{
        [self performSelector:@selector(removeHighlight:) withObject:button afterDelay:0.0];
    }
}

- (void)highlight:(UIButton *)button{
    button.selected = !button.selected;
    button.highlighted = YES;
}

- (void)removeHighlight:(UIButton *)button{
    button.selected = !button.selected;
    button.highlighted = NO;
}

Upvotes: 0

Bob de Graaf
Bob de Graaf

Reputation: 2722

This is not possible unfortunately, as soon as you let go Apple changes the state again. One option I've used before (but not pretty) is that you use a performSelector with delay 0.1 after the button is pressed to put it again in the selected state. This did work in my case.

EDIT: If you don't want to see the blinking effect, just set the delay to 0.0

Upvotes: 0

Kwame
Kwame

Reputation: 1097

I had this same problem, and this is what worked for me:

-(IBAction)buttonPressed:(id)sender {
if (isSelected) {
    [_button setSelected:NO];
    [_button setImage:[UIImage imageNamed:@"not_selected.png"] forState:UIControlStateSelected]; 
    isSelected=NO;

}
else {
    [_button setSelected:YES];
    [_button setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
    isSelected=YES;
}
}

Upvotes: 1

jrturton
jrturton

Reputation: 119242

The highlighted state is applied and removed by iOS when you touch / release the button. So don't depend on it in your action method.

As one of the other answers says, set the highlighted image to be the same as the selected image, and then modify your action method:

-(IBAction) emergencyButtonPress:(id) sender 
{
    emergencyButton.selected = !emergencyButton.selected;     
} 

Upvotes: 6

phi
phi

Reputation: 10733

If you want something like a toggle button, customize your "selected" state to be your "pressed" state, and then write something like this:

- (IBAction)buttonTapped:(id)sender {
    [(UIButton*)sender setSelected:![sender isSelected]];
}

Upvotes: 2

holographix
holographix

Reputation: 2557

if you want the above code to work, you should set an image for the state selected and one (even the same) for he state highlighted.

[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateHighlighted];
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateSelected];

hope it helps.

Upvotes: 1

Related Questions