Reputation: 2181
I intend to dismiss my current UIViewController
and present to a new UIViewController
.
I used the following code
let newViewController: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
self.presentViewController(newViewController, animated: false, completion: {
self.dismissViewControllerAnimated(false, completion: nil)
})
It gave the following error
2016-06-04 11:40:59.864 myApp[851:117649] Trying to dismiss the
presentation controller while transitioning already. (<_UIFullscreenPresentationController: 0x1703e6900>) 2016-06-04 11:40:59.878 ePassBook[851:117649] transitionViewForCurrentTransition is not set, presentation controller was dismissed during the presentation? (<_UIFullscreenPresentationController: 0x1703e6900>)
Upvotes: 5
Views: 6847
Reputation: 1531
Try to this code I think it's Helpfully.
OBJECTIVE-C
[self.navigationController presentViewController:newViewController animated:NO completion:^{
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
[self.navigationController dismissViewControllerAnimated:NO completion:nil];
});
}];
SWIFT
self.navigationController?.presentViewController(newViewController, animated: false, completion: { () -> Void in
dispatch_async(dispatch_get_main_queue()) {
self.navigationController?.dismissViewControllerAnimated(false, completion: nil)
}
})
Upvotes: 1
Reputation: 27448
You first need to dismiss currently present viewcontroller
then after you only present another because at a time you can't present two viewcontrollers
so, your code should be like,
self.dismissViewControllerAnimated(false) { () -> Void in
self.presentViewController(newViewController, animated: true, completion: nil)
}
If you are using navigation controller
then present or dismiss VC on navigation controller
Update as asked in comment:
Take an example,
You have three View controllers say A,B and C and currently presented viewController is C. like A - > B - > C
Now you want to dismiss C and present new D then you must make instance of B because you are dismissing C, so self means nothing. It's nil.
So you should make instance of B lets say b
and present D on that b
Something like,
self.dismissViewControllerAnimated(false) { () -> Void in
b.presentViewController(d, animated: true, completion: nil)
}
If you are using navigation controller then it should be like,
b.navigationController.presentViewCon.....
Upvotes: 0
Reputation: 3245
Use this Code,
Objective C Code:
[self.navigationController presentViewController:newViewController animated:NO completion:^{
dispatch_after(0, dispatch_get_main_queue(), ^{
[self.navigationController dismissViewControllerAnimated:NO completion:nil];
});
}];
Swift Code:
self.navigationController?.presentViewController(newViewController, animated: false, completion: { () -> Void in
dispatch_after(0, dispatch_get_main_queue(), { () -> Void in
self.navigationController?.dismissViewControllerAnimated(false, completion: nil)
})
})
hope its helpful
Upvotes: 1