if Condition is True before it performs segue

老子叫甜甜 提交于 2019-12-11 22:27:34

问题


I am using segue for the transition from ViewController B going to ViewController A and at the same time I am using segue to pass data from ViewController B going to ViewController A. I already constructed my codes wherein the user will tapped the backbutton and executes an alert controller using SCLAlertView cocoapods. It prompts before it proceeds to the next ViewController, but when I run it, the segue performs automatically without checking the condition first and it did not executes the alert controller. My codes are below for your reference.Hope you could help me regarding this because I am new in swift and I had my research but seems I can't see solution that is applicable in my issue. Thank you.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showVCA" {
        if let vc_A = segue.destination as? ViewControllerA {
            vc_A.validPincode = validPincode
            vc_A.participants = participants
            vc_A.event = event
        }
    }
}



 @IBAction func backbutton(_ sender: UIButton) {
    let alert = SCLAlertView(appearance: confirmationAppearance)
    _ = alert.addButton("Leave", action: {
        self.dismiss(animated: true, completion: nil)
        self.performSegue(withIdentifier: "showVCA", sender: sender)

    })
    _ = alert.addButton("Stay", action: { })
    _ = alert.showError("Confirmation", subTitle: "Are you sure you want to leave this page?")

}

回答1:


Validation should be done before performSegue, once you performSegue means you already execute the code to move to the next ViewController.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showVCA" {
        if let vc_A = segue.destination as? ViewControllerA {
            vc_A.validPincode = validPincode
            vc_A.participants = participants
            vc_A.event = event
        }
    }
}



@IBAction func backbutton(_ sender: UIButton) {
    let alert = SCLAlertView(appearance: confirmationAppearance)
    _ = alert.addButton("Leave", action: {
        self.dismiss(animated: true, completion: nil)
        if validation() {
            self.performSegue(withIdentifier: "showVCA", sender: sender)
        }

    })
    _ = alert.addButton("Stay", action: { })
    _ = alert.showError("Confirmation", subTitle: "Are you sure you want to leave this page?")

}

func validation() -> Bool {
     // add validation logic here
     return false
}



回答2:


Your backbutton() function doesn't appear to present the alert controller (by calling one of SCLAlertView's many showXXX() functions), so nothing will ever be shown. And is it possible that your button is connected to your view controller's Exit instead of the action?



来源:https://stackoverflow.com/questions/52921717/if-condition-is-true-before-it-performs-segue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!