Home > Net >  Perform Segue is not working when i am using programmatic UI
Perform Segue is not working when i am using programmatic UI

Time:03-01

I tried to performSegue from one controller to another viewController, i already add the "identifier" in my segue enter image description here

And this is how i called the "performSegue" from my SecondViewController: enter image description here

But everytime i run the app and click the button,it always gave me this error: enter image description here

Do i miss something here? I am a beginner by the way, i hope you guys can help me. Thank you

CodePudding user response:

When posting questions here, it would be good for you to take a few minutes to review How to Ask

However, based on the little bit of info you've provided...

Almost certainly the problem is that you are adding View Controllers in Storyboard and then improperly trying to use them via code.

For example, I'm guessing that you have code in your "first" view controller to load and display SecondViewController like this:

@objc func showSecondTapped(_ sender: Any) {
    let vc = SecondViewController()
    navigationController?.pushViewController(vc, animated: true)
}

and then in SecondViewController you're trying to use the Storyboard associated segue with this:

@objc func btnPressed(_ sender: Any) {
    performSegue(withIdentifier: "gotoBla", sender: self)
    print("button pressed")
}

However, that segue doesn't exist as part of SecondViewController code ... it is part of the Storyboard object.

Back in your first view controller, if you load and push to SecondViewController like this:

@objc func showSecondTapped(_ sender: Any) {
    if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
        navigationController?.pushViewController(vc, animated: true)
    }
}

you will then be able to call performSegue because you loaded it from the Storyboard.

CodePudding user response:

        if let vc = storyboard?.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController {
            
        navigationController?.pushViewController(vc, animated: true)


         // if navigationController?.pushViewController doesn't work you can try this
        present(vc, animated: true) {
            // anything you want to perform after presenting new screen
        }
// and try this to show in full screen with different transition effect
        
        vc.modalTransitionStyle = .crossDissolve
        vc.modalPresentationStyle = .fullScreen
        
        present(vc, animated: true) {
            // anything you want to perform after presenting new screen
           }
}
  • Related