Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Swift Xcode跳过if-else语句_Swift_Segue_Uisegmentedcontrol_Uialertcontroller - Fatal编程技术网

Swift Xcode跳过if-else语句

Swift Xcode跳过if-else语句,swift,segue,uisegmentedcontrol,uialertcontroller,Swift,Segue,Uisegmentedcontrol,Uialertcontroller,我的if-else语句检查某些文本字段是否为空,如果为空,则弹出警报。然而,xcode即使完成了所有工作,也会转到其他函数 有一个if语句,它检查分段控件的值,并相应地检查一些文本字段 @IBAction func calc(_ sender: Any) { // Check if dilution text field is empty let dilutiontext = self.dilution.text if (dilutiontext?.isEmpt

我的if-else语句检查某些文本字段是否为空,如果为空,则弹出警报。然而,xcode即使完成了所有工作,也会转到其他函数

有一个if语句,它检查分段控件的值,并相应地检查一些文本字段

@IBAction func calc(_ sender: Any) {
    
    // Check if dilution text field is empty
    let dilutiontext = self.dilution.text
    if (dilutiontext?.isEmpty ?? true) {
        Alert.showAlert(on: self, with: "Empty Fields", message: "Dilution field is empty")
    }
    if choose.selectedSegmentIndex == 0 {
        
        if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true {
            Alert.showAlert(on: self, with: "Empty Fields", message: "Number 1-4 fields should not be empty")
        } else {
            performSegue(withIdentifier: "turner", sender: self)
        }
    } else {
        if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.number5.text?.isEmpty ?? true || self.number6.text?.isEmpty ?? true || self.number7.text?.isEmpty ?? true || self.number8.text?.isEmpty ?? true {
            Alert.showAlert(on: self, with: "Empty Fields", message: "Number 1-8 fields should not be empty")
        } else {
            performSegue(withIdentifier: "turner", sender: self)
        }
    }
    
}
我有另一个控制警报的文件alert.swift:

import Foundation
import UIKit

struct Alert {
    public static func showAlert(on vc: UIViewController, with title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        vc.present(alert, animated: true)
    }
}
编辑:

以前是self.dilution.text?.isEmpty,现在让dilutiontext=self.dilution.text和dilutiontext?isEmpty

我注释掉了prepare for segue功能,令人惊讶的是警报开始工作。我仍然需要该功能和警报工作虽然。以下是函数:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    var vc = segue.destination as! SecondViewController
    
    if choose.selectedSegmentIndex == 0 {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.dil = Int(dilution.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)

    } else {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.n5 = Int(number5.text!)!
        vc.n6 = Int(number6.text!)!
        vc.n7 = Int(number7.text!)!
        vc.n8 = Int(number8.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)
        vc.dil = Int(dilution.text!)!
    }

}
当我运行它时,它不会显示警报(检查文本字段是否为空),而是继续执行segue函数,并在展开可选值时显示意外的find nil,这是预期值,显然“if”和“else if”条件都不是真的。加

let dilutiontext = self.dilution.text
let celltext = self.cell.text

然后设置断点并检查值。

显然,如果segue函数中的一个if条件为true,则跳过警报。因此,如果一开始有什么东西会使这些声明成为虚假的,那么在经过警报之后,它会使它们成为真实的,那么问题就解决了

因此,我在segue func中为if和if else语句分别创建了两个函数

func option1() -> Bool {
    if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.dilution.text?.isEmpty ?? true || !(self.number5.text?.isEmpty ?? true) || !(self.number6.text?.isEmpty ?? true) || !(self.number7.text?.isEmpty ?? true) || !(self.number8.text?.isEmpty ?? true) {
        return false
    } else {
        return true
    }
}

func option2() -> Bool {
    if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.number5.text?.isEmpty ?? true || self.number6.text?.isEmpty ?? true || self.number7.text?.isEmpty ?? true || self.number8.text?.isEmpty ?? true || self.dilution.text?.isEmpty ?? true {
        return false
    } else {
        return true
    }
}
检查所有条件是否为真,如果为真,则返回真,以便程序可以继续执行segue func

func option1() -> Bool {
    if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.dilution.text?.isEmpty ?? true || !(self.number5.text?.isEmpty ?? true) || !(self.number6.text?.isEmpty ?? true) || !(self.number7.text?.isEmpty ?? true) || !(self.number8.text?.isEmpty ?? true) {
        return false
    } else {
        return true
    }
}

func option2() -> Bool {
    if (self.number1.text?.isEmpty) ?? true || self.number2.text?.isEmpty ?? true || self.number3.text?.isEmpty ?? true || self.number4.text?.isEmpty ?? true || self.number5.text?.isEmpty ?? true || self.number6.text?.isEmpty ?? true || self.number7.text?.isEmpty ?? true || self.number8.text?.isEmpty ?? true || self.dilution.text?.isEmpty ?? true {
        return false
    } else {
        return true
    }
}
segue将检查条件是否为真,如果不是,它将通过警报,从而使option1()或option2()返回真-因此segue func中的if条件将为真,以便程序继续

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    var vc = segue.destination as! SecondViewController

    if option1() == true {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.dil = Int(dilution.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)

    } else if option2() == true {
        vc.n1 = Int(number1.text!)!
        vc.n2 = Int(number2.text!)!
        vc.n3 = Int(number3.text!)!
        vc.n4 = Int(number4.text!)!
        vc.n5 = Int(number5.text!)!
        vc.n6 = Int(number6.text!)!
        vc.n7 = Int(number7.text!)!
        vc.n8 = Int(number8.text!)!
        vc.cn = Int(choose.selectedSegmentIndex)
        vc.dil = Int(dilution.text!)!
    }

}

谢谢你的帮助。我确实检查了if语句是否正确,我还对所有if、else if和else使用了警报,但仍然没有出现任何警报。还在想办法吗