2

I have a button in my SettingsViewController that when pressed, I want to present the same instance of my TimerViewController each time the button is pressed.

[This is what it looks like]

I think I have gotten fairly close with this post here, iOS Swift, returning to the same instance of a view controller. So if you could point me to saving the instance of TimerViewController, that would work the best.

This is the code I have right now -

var yourVariable : UIViewController!

if yourVariable == nil {
   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   yourVariable = storyboard.instantiateViewControllerWithIdentifier("timer") as! TimerInterface
        }
presentViewController(yourVariable, animated: true, completion: nil)
Community
  • 1
  • 1
zennox
  • 624
  • 1
  • 8
  • 19
  • 1
    what is the problem with the code? – André Slotta Nov 26 '15 at 18:39
  • I need a way to go back to the same instance of `TimerViewController`. I have a good way of instantiating first, but I don't know how to return back to the same instance. – zennox Nov 26 '15 at 18:42
  • Your code should work. – Tim Nov 26 '15 at 18:44
  • 1
    but this is exactly what the code you posted does! the problem is that if your `SettingsViewController` itself gets dismissed the saved instance of the `TimerViewController` also gets deallocated and recreated the next time... – André Slotta Nov 26 '15 at 18:45
  • @AndréSlotta I think you're right. How do I make sure that `SettingsViewController` doesn't get dismissed? And I was in the understanding that `instantiateViewControllerWithIdentifier("timer")` creates a new instance each time. – zennox Nov 26 '15 at 18:53
  • i think the way to go is something else... what exactly happens in your `TimerViewController`? – André Slotta Nov 26 '15 at 18:57
  • add a `print` line with the `instantiate` line. I think you'll see it's not being called again. – Tim Nov 26 '15 at 19:02
  • @Tim @AndréSlotta Yes, I did manage to get it working. The `instantiate` was working, but it was being run again due to the new `SettingsViewController` being created. Thanks again! – zennox Nov 26 '15 at 19:08

2 Answers2

4

the code you provided should work. if your SettingsViewController gets deallocated though the timerViewController also gets deallocated and recreated the next time you present it. so you have to make sure to save its instance at an appropriate location.

var timerViewController: TimerViewController!

if timerViewController == nil {
   let timerViewController = UIStoryboard(name: "Main", bundle: nil)
   yourVariable = storyboard.instantiateViewControllerWithIdentifier("timer") as! TimerInterface
}

presentViewController(timerViewController, animated: true, completion: nil)
André Slotta
  • 13,774
  • 2
  • 22
  • 34
  • Thanks so much! I dismissed the TimerViewController, which fixed a problem where I was creating a new instance of SettingsViewController. The instantiate was working, but it was being run again due to the new SettingsViewController being created. – zennox Nov 26 '15 at 19:09
0

The best would be to save the ViewController somewhere , and get back to it . A way to "get back to it" : add

var tvc: TimerViewController? = nil

inside AppDelegate when you get to your Timer (the best would be when you left it , in viewDidDisappear)

you add :

(UIApplication.sharedAplication().delegate as! AppDelegate).tvc = self

then when you get to the setting , if you want to segue back to the timer

let tvc = (UIApplication.sharedAplication().delegate as! AppDelegate).tvc
(UIApplication.sharedApplication().delegate? as! AppDelegate).window?.rootViewController?.presentViewController(tvc, animated: true , completion: nil)

if you ask yourself why should you present it with the rootViewController (last line ) it is because you can present an already active viewController , this will not present an already active vc .

kholl
  • 609
  • 1
  • 6
  • 20
  • Thanks for the answer, I did manage to get it working with the accepted response. I hope this can help others if they run into this problem! – zennox Nov 26 '15 at 19:12