0

When I start the app my initial view is MainViewController.
Then on button click I move to AnotherViewController.

performSegueWithIdentifier("myFirstSegue", sender: self)

On AnotherViewController I have a button that should return me back to MainViewController but I don't want to instantiate new but to use existing instance.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
var story = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! ViewController
let navigationController = UINavigationController(rootViewController: story)
self.showViewController(navigationController, sender: nil)

With this code I create new so I lost data that I stored on that view.

How can I get existing instance and just move to it?

UPDATE

To explain issue better if on first view I have UISwitch and move to another ViewController and then get back to first I want to UISwitch stay same not "reset".

EI Captain v2.0
  • 21,360
  • 10
  • 76
  • 103
1110
  • 7,801
  • 45
  • 156
  • 303

3 Answers3

0

if you are using a navigation controller, doing this will pop to your previous controller. navigationController.popViewControllerAnimated(true)

Lucho
  • 949
  • 13
  • 20
  • No. I don't use navigation controller. I want to make same effect like you change tabs on TabBarController. – 1110 Sep 11 '15 at 13:12
0

you can use this one

self.dismissViewControllerAnimated(false, completion: nil);
Bhanu
  • 1,211
  • 9
  • 17
0

Navigation controller maintains the stack of viewcontrollers. just pop your viewcontroller and you will be back to your previous view controller.

navigationController.popViewControllerAnimated(true)

or use unwind segue from storyboard.

What are Unwind segues for and how do you use them?

Community
  • 1
  • 1
Viren Malhan
  • 115
  • 1
  • 5