Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-在特定条件下执行性能检查_Ios_Swift_Xcode - Fatal编程技术网

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
}