如何创建一个允许我执行2个IBAction iOS的UIButton - Objective-C

时间:2015-12-18 18:31:38

标签: ios objective-c uibutton

如何创建一个允许我执行2 UIButton的{​​{1}}。例如,在用户按下"关注"按钮变为"取消关注"反之亦然。最好用2个按钮或1个按钮完成并使用不同的状态。我已经研究过如何用一个按钮来完成这个,但是没有找到任何解决方案。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用一个按钮实现这一目标。您需要为每个按钮的states设置按钮的标题,并在选择或取消选择时切换IBAction方法中的状态:

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

并实施IBAction方法,如:

- (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
   }
}