2

I've noticed that if I do something like this

[self setTitle:@"Follow" forState:UIControlStateNormal];
[self setTitle:@"Following" forState:UIControlStateSelected];

The button will start off with "Follow". I press it, and when I release my finger (touch up event) it changes to "Following". That's great. However, when It's now selected and I press it, it immediately changes to "Follow" before I release my finger (on a touch down event). I want to change when I release my finger (touch up event). How do I do this? I am using UIControlStates for a UIButton's title, images, and title color.

Below is my event handler:

- (void)followButtonAction:(UIButton *)button
{
    button.selected = !button.selected;
}

and I set it as so:

[self.followButton addTarget:self action:@selector(followButtonAction:) forControlEvents:UIControlEventTouchUpInside];
JVillella
  • 1,029
  • 1
  • 11
  • 21
  • then why are you using "touch down event" use "touch up event" for both? – Malav Soni May 02 '15 at 02:48
  • try to set UIControlStateHighlighted also.. – Malav Soni May 02 '15 at 02:49
  • The button.selected is set in a touch up event. That is what makes this behaviour odd. And I have tried the UIControlStateHighlighted state on its own and combined with UIControlStateSelected. No luck. – JVillella May 02 '15 at 02:50
  • It seems like button.state and button.selected do not change at the same time. Irregardless of when button.selected is changed. button.state is set to UIControlStateSelected during the touch up, and UIControlStateNormal during the touch down. – JVillella May 02 '15 at 02:52

2 Answers2

0

try to use this, This works for me as toggle button.

-(IBAction)buttonTapped:(UIButton *)sender{
    if ([sender isSelected]) {
        [sender setSelected:NO];
    }else{
        [sender setSelected:YES];
    }
}

Hope this works for you too.

Malav Soni
  • 2,739
  • 1
  • 23
  • 52
0

Turns out the solution is to add another control state specifically for the combination that is causing the problem (when selected, and then being pressed). Below is the solution:

[self setTitle:@"Follow" forState:UIControlStateNormal];
[self setTitle:@"Following" forState:UIControlStateSelected];
[self setTitle:@"Following" forState:UIControlStateSelected | UIControlStateHighlighted];
JVillella
  • 1,029
  • 1
  • 11
  • 21