gpt4 book ai didi

ios - Swift - 在条件下执行 performSegue

转载 作者:行者123 更新时间:2023-11-28 13:43:05 25 4
gpt4 key购买 nike

我的意图是仅在所有文本字段都已填充时运行 performSegue。如果不是,则该按钮不应工作 - 确切地说,不应执行 performSegue

我的方法是将 performSegue 放在 if 语句中,但不知何故它被忽略了并且 performSegue 仍然被执行,即使两个字段都是空的。还有其他更成功的方法吗?

@IBAction func buttonAdd(_ sender: Any) {
if (addKmInput.text! != "" && addPriceInput.text != "") {
...
performSegue(withIdentifier: "goBackToSecond", sender: self)
}
}
@IBOutlet weak var addKmInput: UITextField!
@IBOutlet weak var addPriceInput: UITextField!

新版本:

@IBAction func buttonAdd(_ sender: Any) {        
performSegue(withIdentifier: "goBackToSecond", sender: self)
}

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
switch identifier {
case "goBackToSecond":
return shouldGoBackToSecond()
default:
return true
}
}

func shouldGoBackToSecond() -> Bool {
guard let kmInput = addKmInput.text, let priceInput = addPriceInput.text else { return false }
return !kmInput.isEmpty && !priceInput.isEmpty
}

最佳答案

尝试以下解决方案:

@IBAction func buttonAdd(_ sender: Any) {
if shouldGoBackToSecond() {
performSegue(withIdentifier: "goBackToSecond", sender: self)
}
}

func shouldGoBackToSecond() -> Bool {
guard let kmInput = addKmInput.text, let priceInput = addPriceInput.text else { return false }
return !kmInput.isEmpty && !priceInput.isEmpty
}

关于ios - Swift - 在条件下执行 performSegue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55775731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com