Alex Alex - 5 months ago 16
Swift Question

How to perform actions based on segue

Here's what I'm trying to do:

I have 2 views, Settings and Main Screen.

I've coded a segue to occur when a button in Settings is pressed, taking the view to the Main Screen. The identifier is "reset".

I'm trying to have the Main Screen perform a series of actions if this segue is triggered, but I can't find the function or way to do this.

Any help on how to implement this? I'd like it to trigger when the segue occurs.

Answer

You can pass arguments to the main screen in the prepareForSegue function in the settings page. Then in your main screen you can put in checks in your viewWillAppear function to handle them as you see fit.

Example:

In Settings:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "reset") {
        // pass data to next view
        let viewController:ViewController = segue!.destinationViewController as MainViewController
        viewController.settings = settings // where settings is what you want to pass
    }
}

In Main Screen:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    if self.settings { // Do something }
}