在 segue 函数上设置条件 swift

Set condition on a segue function swift

In a single button, i put both alart system and a pop up view. If the text fields are blank then it will send an alart. If everything is okay then it will show popup view. Alart system and pop up is conflicting. How do I set conditon to segue function when to perform or not?

函数没有写好。我能够了解。但是我很困惑写它。我如何调用覆盖函数?

func stateCondition(state: Bool) {
    if state {
        print("Entered into the state") //entering into the func
        func prepare(for segue: UIStoryboardSegue, sender: Any?) {

            if segue.identifier == "info"{
                _ = segue.destination as! InfoViewController
            }
            if segue.identifier == "popUp" {
                let vc = segue.destination as! PopUpViewController
                vc.age = Double(ageTextField.text!)!
                vc.gender = genderTextField.text!
                vc.bmi = Double(bmiLabel.text!)!

                print("Entered into the segue") //is not entering into this func

            }
        }
    }
}

有一个普遍的误解:你不能调用 prepare(for segue – 无论如何语法都是错误的 – 该函数是在执行 segue 之前由框架调用的。

你的意思可能类似于

func stateCondition(state: Bool) {
    if state {
        print("Entered into the state") //entering into the func
        performSegue(withIdentifier: "info", sender: nil)
    } else {
        performSegue(withIdentifier: "popUp", sender: nil)
    }
}

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "popUp" {
        let vc = segue.destination as! PopUpViewController
        vc.age = Double(ageTextField.text!)!
        vc.gender = genderTextField.text!
        vc.bmi = Double(bmiLabel.text!)!

        print("Entered into the segue") //is not entering into this func
    }
}

尝试阅读代码和注释。 首先,方法func prepare(for segue: UIStoryboardSegue, sender: Any?)不能在另一个方法中。您设法以某种方式将其压缩在 func stateCondition(state: Bool) 中。 其次 你没有在任何地方打电话给 performSegue(withIdentifier: identifier, sender: self)。您可能应该 :) 查看代码,希望对您有所帮助。我记得我的第一个 segue,我花了一些时间才明白发生了什么。

class ViewController: UIViewController {

    @IBOutlet private var ageTextField: UITextField!
    @IBOutlet private var genderTextField: UITextField!
    @IBOutlet private var bmiLabel: UILabel!

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // 3
        // This method gets called and there you do your stuff with respective VCs
        if segue.identifier == "info", let infoViewController = segue.destination as? InfoViewController {
            // 3.1
            // If the identifer is set to INFO then you go to the InfoViewController and assigne message
            infoViewController.message = "Some fields are empty"

        } else if segue.identifier == "popUp", let popUpViewController = segue.destination as? PopUpViewController {
            // 3.2
            // If the identifer is set to POPUP then you go to PopUpViewController and assign age, gender and bmi
            popUpViewController.age = "33"
            popUpViewController.gender = "male"
            popUpViewController.bmi = "20"
        } else {
            print("Identifer is none of the above")
        }
    }

    @IBAction private func buttonTapped(_ sender: Any) {

        // 1.
        // First you need to figure out where you want to take the user
        // You do that in the method getSegueIdentifier() where you get the identifier
        let identifier = getSegueIdentifier()

        // 2.
        // Then you performSegue with that identifer
        performSegue(withIdentifier: identifier, sender: self)
    }

    private func getSegueIdentifier() -> String {

        if ageTextField.text?.isEmpty == true && genderTextField.text?.isEmpty == true && bmiLabel.text?.isEmpty == true {
            return "info"
        } else {
            return "popUp"
        }
    }
}

class InfoViewController: UIViewController {

    var message = ""

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        showAlert()
    }

    func showAlert() {
         // show alert with message
        print(message)
    }
}

class PopUpViewController: UIViewController {

    var age = ""
    var gender = ""
    var bmi = ""

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        showPopUp()
    }

    func showPopUp() {
        // show popUp with age gender and bmi
        print(age, gender, bmi)
    }
}