如何防止导航到UIViewController检查条件?

Aug*_*sto 1 xcode uinavigationcontroller ios uistoryboardsegue swift

我有一个显示视图控制器的按钮(ViewControllerIncluirItem).但是,我需要在导航之前检查一个条件,我尝试在func中检查这个override func prepare(for segue: UIStoryboardSegue, sender: Any?).但是,导航只能以任何方式发生.我试过这个:

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

        if segue.identifier == "SegueToVcIncluirItem" {

            if pricelist != nil {
                print("pricelist ok")              
            } else {
                print("selecione pricelist")
                return // Here I want prevent.
            }

            let nav = segue.destination as! UINavigationController
            let childVc = nav.topViewController as! ViewControllerIncluirItem
            childVc.strTeste = "testado com sucesso"
        }
    }
Run Code Online (Sandbox Code Playgroud)

Enr*_*dez 6

您可以在方法shouldPerformSegue(withIdentifier:sender :)中检查您的条件

您的代码应如下所示:

    func shouldPerformSegue(withIdentifier identifier: String,
                            sender: Any?) -> Bool{

        if identifier == "SegueToVcIncluirItem" {
           return pricelist != nil
        }

        return true
    }
Run Code Online (Sandbox Code Playgroud)