1

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.

Alex
  • 141
  • 2
  • 10
  • 1
    By views do you mean controller because segue are used between controller ? And why don't you call your function in the `viewDidAppear` (or `viewWillAppear`) of your MainScreen ? – Chajmz Jun 30 '16 at 16:48

2 Answers2

2

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 }
}
Wyetro
  • 8,439
  • 9
  • 46
  • 64
0

If you're wanting something to happen when the segued view controller is triggered you can do it in a couple of places.

If you want the event to happen at the time the segue is fired, perform your code inside of prepareForSegue. There you can check the UIStoryboardSegue object's identifier field for "reset" and if true, call your logic.

If you want it to happen when the destination view controller loads or appears, do it inside of its viewDidLoad or viewDidAppear methods. In your case those methods would exist on the "Main Screen" view controller class.

BlueBear
  • 7,469
  • 6
  • 32
  • 47