Reputation: 7524
My transition to the next view is like this:
if let navigationController = navigationController {
if let storyboard:UIStoryboard = UIStoryboard(name: "myStoryboard", bundle: nil) {
if let vc = storyboard.instantiateViewControllerWithIdentifier("myViewController") as? MyViewController {
dispatch_async(dispatch_get_main_queue()) {
navigationController.presentViewController(vc, animated: true, completion: nil)
}
}
}
}
This works fine. I want this kind of transition. But when I call following code in MyViewController, the NavigationController is nil:
if let navigationController = navigationController {
print("yeah i have a nc")
} else {
print("its nil") //this will call
}
When I use navigationController.pushViewController(vc, animated: true)
everything works fine. But I really want the transition. Is this a wrong implementation on my side or is presentViewController
always without a navigationController? If yes, what can I do?
My Controller A is already embedded in a navigationController. I use navigationController.presentViewController to go to MyViewController. And from MyViewController I want to push to a next ViewController C.
Upvotes: 3
Views: 3277
Reputation: 7524
SOLUTION THAT WORKED FOR ME
I don't know why, but when you use the presentViewController you have to define a new(?) root for your navigationController.
In this context I understood Ahmad Fs answer.
if let storyboard:UIStoryboard = UIStoryboard(name: "myStoryboard", bundle: nil) {
if let vc = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as? MyViewController {
if let navController:UINavigationController = UINavigationController(rootViewController: vc) {
dispatch_async(dispatch_get_main_queue()) {
self.presentViewController(navController, animated:true, completion: nil)
}
}
}
}
SWIFT 3
let storyboard = UIStoryboard(name: UIConstants.Storyboards.registration, bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as? YourViewController {
let navigationController = UINavigationController(rootViewController: vc)
DispatchQueue.main.async {
navigationController.present(vc, animated: true)
}
}
I found "my" solution here
Upvotes: 3