Calling segue programmatically not working

2019-03-01 05:12发布

问题:

I am trying to make a splash screen. I have a view that has a background image being drawn onto it and then another view I want to transition to after a few seconds. I am trying to use the following code:

self.performSegueWithIdentifier("showApp", sender: self)

I created a segue between the two views by ctrl+dragging a line from one to the other. I set the segue's identifier to "showApp".

When I run the code nothing happens and there are no errors. Any ideas?

Here is the controller for the splash screen:

class SplashViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        sleep(2)

        // THIS DOES NOTHING:
        self.performSegueWithIdentifier("showApp", sender: self)

        // THIS AS SUGGESTED ALSO DOES NOTHING:
        var otherViewController:UIViewController = self.storyboard.instantiateViewControllerWithIdentifier("test") as UIViewController
        self.presentViewController(otherViewController, animated: true, completion: nil)
    }
}

回答1:

Normally, you need a navigation controller in order to use segue.

Highlight your SplashViewController object inside the Storyboard and go to

Editor -> Embeded In -> Navigation Controller

After that, remove the code suggested by Clement and try running the project again, you should get the result that you expected.



标签: ios swift xcode6