swift - 如何使按钮重置为默认状态?

标签 swift uibutton switch-statement

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

buttonPressed() {
 switch 1
 switch 2
}

开关 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 行开始执行代码?

最佳答案

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

关于swift - 如何使按钮重置为默认状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620264/

相关文章:

c# - 为什么 Visual Studio 2019 推荐使用 switch 表达式而不是 switch 语句?

autocomplete - 安卓工作室 : Enum auto-completion of switch case

swift - 过滤快速用户触摸输入

ios - 如何在 iOS swift 中将 subview 添加到另一个中心对齐的 subview 。

swift - 通过使用 swift 在 firebase 中搜索键来检索值

ios - UIButton 不显示我的数组的 UIbuttons 背景图像

java - 魔数问题的解决方案......?

ios - iOS 中如何设置图像的长宽比

ios - 如何将 UIButton 用作切换按钮?

iphone - 弹跳按钮