Ios Swift-在特定条件下执行性能检查
我的目的是仅在所有文本字段都已填充的情况下运行Ios Swift-在特定条件下执行性能检查,ios,swift,xcode,Ios,Swift,Xcode,我的目的是仅在所有文本字段都已填充的情况下运行performsgue。如果没有,则该按钮不应工作-准确地说,performsgue不应执行。 我的方法是将performsgue放在if语句中,但不知怎么的,它被忽略了,并且performsgue无论如何都在执行,即使这两个字段都是空的。还有其他更成功的方法吗 @IBAction func buttonAdd(_ sender: Any) { if (addKmInput.text! != "" && addPri
performsgue
。如果没有,则该按钮不应工作-准确地说,performsgue
不应执行。我的方法是将
performsgue
放在if语句中,但不知怎么的,它被忽略了,并且performsgue
无论如何都在执行,即使这两个字段都是空的。还有其他更成功的方法吗
@IBAction func buttonAdd(_ sender: Any) {
if (addKmInput.text! != "" && addPriceInput.text != "") {
...
performSegue(withIdentifier: "goBackToSecond", sender: self)
}
}
新版本:
@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
}
使用
str.isEmpty
检查文本字段中的字符串是否为空:
if let t1 = addKmInput?.text, let t2 = addPriceInput?.text, !t1.isEmpty, !t2.isEmpty {
...
performSegue(withIdentifier: "goBackToSecond", sender: self)
}
理想情况下,您应该使用此委托来拒绝segue性能:
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return true //return true/false conditionally. returning false will prevent segue performance.
}
尝试以下解决方案:
@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
}
我是swift新手-我遇到以下错误:
在“警卫”状态后应为“else”
谢谢您的帮助。我已经用新版本的代码更新了最初的问题-不知何故,它仍然执行performSegue并更改ViewControllerLets有一个聊天摘要:通过忽略shouldPreformSegue
函数并在按钮添加(…)
函数处写入if条件,修复了该问题。
@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
}