【问题标题】:IF UIButton was pressed Xcode如果 UIButton 被按下 Xcode
【发布时间】:2011-03-13 15:33:08
【问题描述】:

是否可以有一个 if 语句来查看按钮是否被按下?

if(condition) {
  //Do something
}

条件是什么?

【问题讨论】:

  • 典型的 UI 编程是通过事件处理完成的。也就是说,您绑定一个(或多个)函数以作为 UI 事件的结果执行 - 例如单击按钮。

标签: cocoa-touch ios uibutton


【解决方案1】:

一个 UIButton 将调用一个方法,它在 Interface Builder 或set to in code 中连接到该方法。

如果您需要知道程序的某些部分是否按下了按钮,您可以执行以下操作:

-(IBAction)buttonTapped:(UIButton *) sender
{
    self.buttonPressed = YES; //bool instance variable with property
}

-(void)someOtherMethod
{
    if(self.buttonPressed){
        //do what you want to do, if button pressed
    }
}

但我认为,在语义级别上如此紧密地耦合 UI 和逻辑并不是一个好主意。

【讨论】:

    猜你喜欢
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    相关资源
    最近更新 更多