【问题标题】:How to make a button reset to default state?如何使按钮重置为默认状态?
【发布时间】:2015-04-14 06:09:06
【问题描述】:

我有一个有 2 个 switch 语句的按钮。

buttonPressed() {
 switch 1
 switch 2
}

Switch 1 检查错误。如果没有发现错误,则执行 Switch 2。这部分效果很好。

问题是,当发现错误时,在第二次按下按钮时,会跳过开关 1。

如何让按钮每次按下时总是从头开始执行?

就好像它在切换结束时创建了一个断点,第二次按下从那个断点继续。

下面是 switch1 的摘录:

case 1:
    let oneLength = countElements(textOne.text)
    if oneLength <= 1 {
        self.shouldSave = false
        showAlertWithText(message: "First checklist item must have a value")
    } else if oneLength >= 141 {
        self.shouldSave = false
        showAlertWithText(message: "First checklist item exceeds the 140 character limit")
    } else {
        newChecklistOne["goalID"] = self.dependantGoalID
        newChecklistOne["itemDescription"] = textOne.text
        newChecklistOne["isComplete"] = false
    }

开关 2 示例:

if self.shouldSave == true {
while self.saveVarTwo <= self.z {
    switch self.saveVarTwo {
        case 1: newChecklistOne.saveInBackground()

这里是 showAlertWithText 函数:

func showAlertWithText (header : String = "Ermahgerd!!!", message : String) {
    var alert = UIAlertController(title: header, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)

}

那么,如何让按钮在每次单击按钮时从第 1 行开始执行代码?

【问题讨论】:

  • 如果 self.shouldSave == true { .. } 是多余的。你应该使用 if shouldSave { ... }
  • 怎么冗余?默认状态是设置为真。然后,如果发现错误,我将其更改为 False。然后,该布尔切换确定开关 2 是否执行...
  • if shouldSave { ... } 如果你想测试如果不只是添加!在前面 if !shouldSave { ... }
  • 但这如何解决问题呢?每次按下按钮时,我都希望按钮从按钮功能代码的第 1 行开始。现在,它直接跳到switch2。 shouldSave 工作正常。
  • 这就是我将它作为评论发布的原因。要时刻铭记在心。就是这样。

标签: swift uibutton switch-statement


【解决方案1】:

所以,我有一个“saveVar”,它用作开关 1 的开关催化剂。它会迭代到条目数,但在发生错误时我从不重置它。因此,当按下按钮时,开关在最大情况下启动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 2012-03-15
    • 2012-05-21
    • 1970-01-01
    • 2021-04-11
    • 2023-03-26
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多