Alex Knows
Alex Knows

Reputation: 11

How can I create one UIButton that allows me to perform 2 IBAction iOS - Objective-C

How can I create one UIButton that allows me to perform 2 IBAction. For example after a user presses "Follow" the button turns to "Unfollow" and vice a versa. Is this best to accomplish with 2 buttons or 1 button and using different states. I've research how to acomplish this with one button but haven't found any solution. Thanks!

Upvotes: 0

Views: 37

Answers (1)

Midhun MP
Midhun MP

Reputation: 107121

You can achieve that using a single button. You need to set the title of your buttons to each of it's states and toggle the states in the IBAction method when a selection or de-selection occurs:

[yourButton setTitle:@"Follow" forState:UIControlStateNormal];
[yourButton setTitle:@"Unfollow" forState:UIControlStateSelected];

And implement the IBAction method like:

- (IBAction)btnClicked:(UIButton *)sender
{
   sender.selected = !sender.selected;
   if (sender.selected)
   {
      // Currently selected the button and it's title is changed to unfollow
      // Do your selection action here
   }
   else
   {
      // Currently de-selected the button and it's title is changed to follow
      // Do your selection action here
   }
}

Upvotes: 1

Related Questions