kuzdu kuzdu - 1 month ago 17
Swift Question

presentViewController has no navigationController swift

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.

Answer

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)
                }
            }
        }
    }

I found "my" solution here