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

标签 ios objective-c uibutton

如何创建一个 UIButton 来允许我执行 2 个 IBAction。例如,用户按下“关注”后,按钮将变为“取消关注”,反之亦然。最好使用 2 个按钮还是 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
   }
}

关于ios - 如何创建一个 UIButton 来允许我执行 2 个 IBAction iOS - Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34361875/

相关文章:

ios - 如何从 iOS 中的字符串中删除第三个逗号 (,) 后的字符串?

ios - Swift 将 AnyObserver 绑定(bind)到 Observable

ios - Swift - 如何隐藏未知按钮?

ios - 按钮不会从 swift iOS 的 super View 中删除文本字段及其本身

objective-c - Cydia 应用程序文档文件夹未创建

ios - 将扩展转换为类的函数

ios - 防止 initWithContentsOfURL : from using cache

iphone - 禁用 iPhone 屏幕某些区域的触摸事件

objective-c - 游戏中心特定配对

objective-c - 按钮setHidden在iOS 5中崩溃