ios - 检查 UIButton 是否被按下

标签 ios uibutton

我有四个 UI 按钮,我希望根据用户按下的按钮发生不同的事情。我尝试使用 bool 值来检查按钮是否被按下,但它似乎不起作用。每个按钮的 bool 代码基本上是:

 -(IBAction)FirstChoice:(id)sender
{
 wasClicked = YES;
}

然后在主函数本身中:

if(wasClicked)
{
    returnView.text = @"Test";
}

但是,当我按任何按钮时,测试文本不会出现。

最佳答案

我认为你应该为所有按钮使用一个通用的“Touch up inside” socket 功能......并为每个按钮设置不同的标签......例如

-(IBAction)FirstChoice:(id)sender // common "Touch up inside" action for all four buttons
{
 UIButton *btn=(UIButton *)sender; //assuming that you have set tag for buttons

      if(btn.tag==94)
        {
          //Do any thing for button 1
         }
       else if (btn.tag==93)
        {
          returnView.text = @"Test";
          //Do any thing for button 2
         }
       else if (btn.tag==92)
        {
          //Do any thing for button 3
         }
        else
           {
             //Do any thing for button 4

            }


}

关于ios - 检查 UIButton 是否被按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20296605/

相关文章:

ios - 从另一个按钮按下控制按钮的各个方面 - Swift

iphone - iOS应用程序删除: What Remains?

ios UIView initWithCoder 加载太快

iphone - 如何在 iphone 中创建具有 Marquee Effect 的动态多个 uiview

ios - 如何获取xcode默认图像

ios - UIButtons 在旋转到横向然后回到纵向后停止工作

ios - 如何更改 iOS 7 中的中心标签栏按钮颜色?

ios - UIButton 文本动画不动画

ios - 既有内部Touch Up又有触摸重复的UIButton?

ios - 来自 NSString 的 UITableViewCell Nib