如何创建一个允许我执行2 UIButton
的{{1}}。例如,在用户按下"关注"按钮变为"取消关注"反之亦然。最好用2个按钮或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
}
}