Failed to save UIButton in selected state after TouchUpInside event

I need UIButton to support pressed state. In principle, if the button is in normal condition, I want to touch the button, it is highlighted in standard blue, and then remains blue after raising a finger. I did the following UIAction and connected the Touch Up Inside buttons 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 returns to a white background. For the test, I added UISwitch and executed 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 switches to the highlighted and unselected state, as I expected.

Was there another event after the Touch Up Inside event that returns the state to its normal state? How to save the selected state?

+5
source
7

iOS / . .

, , , :

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

- , "" "" , - :

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

, , ( ) .

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

, .

+1

, :

-(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;
}
}
+1

This is not possible, unfortunately, as soon as you release Apple, Apple will again change its state. One of the options that I used earlier (but not very) is that you use the performSelector function with a delay of 0.1 after pressing the button to re-enable the selected state. This worked in my case.

EDIT: If you do not want to see the blinking effect, just set the delay to 0.0

0
source

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;
}
0
source

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

    }
 }
0
source

All Articles