1

I am building an app to present UIViewController and dismiss the current UIViewController at the same time.

Currently I'm in viewcontroller1 and i want to present viewcontroller2 as main controller and dismiss viewcontroller1 to remove it from memory, so viewcontroller2 will be the main controller after presenting and there is no viewcontroller1 in stack.

I've tried :

let controller = storyboard?.instantiateViewControllerWithIdentifier("controller2") as! viewcontroller2
controller.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve

self.dismissViewControllerAnimated(false) { () -> Void in

    self.presentViewController(controller, animated: false, completion: nil)

}

But it gave me : 0x7fee28711900> whose view is not in the window hierarchy!

Is there is a way to do that ?

Nata Mio
  • 2,168
  • 3
  • 22
  • 46

1 Answers1

0

If you want to replace a UIViewController entirely, you should do this:

 UIApplication.sharedApplication().keyWindow?.rootViewController = newController

This however will not cross-dissolve.

If you need to cross, you'll just have to present the new view controller "over" the old one, or follow the advice in this post and re-write it Swift:

https://stackoverflow.com/a/8248284/233602

Community
  • 1
  • 1
Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • where do i do that ? in viewDidLoad of second presented controller ? so by applying your code it will dismiss the old view controller from memory ? – Nata Mio Oct 19 '15 at 07:51
  • I would recommend you centralise your logic for presenting view controllers - perhaps in your app delegate, or better, in a dedicated controller class, and put the above code in there. – Andrew Ebling Oct 19 '15 at 07:53
  • Updated answer with a link to a post showing you how to animate. – Andrew Ebling Oct 19 '15 at 08:01