3

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.

kuzdu
  • 7,124
  • 1
  • 51
  • 69
  • Do you want to present a viewController that connected with navigationController? so, after presenting, you can push and pop... is this the case? – Ahmad F Nov 02 '16 at 15:42
  • Yes, this is the case. My Controller A is already embed in a navigationController. I use navigationController.presentViewController to get MyViewController. And from MyViewController I want push to a next ViewController – kuzdu Nov 02 '16 at 15:46
  • So, I'm assuming that if you tried to navigate to another viewController from the presented controller, it will present instead of push, is it right? – Ahmad F Nov 02 '16 at 15:49
  • What do you mean with "it will present instead of push"? When I use push, everything is fine. I have a navigationController in my next VC. When I use presentViewController I didn't have. – kuzdu Nov 02 '16 at 15:51
  • could you take a screenshot of your storyboard? it will help alot! – Ahmad F Nov 02 '16 at 15:52
  • Subject to [this](http://meta.stackoverflow.com/questions/289344/how-to-treat-an-old-question-which-had-an-answer-edited-into-it) meta post : You should rollback your question to it's original form. If you like you can write an answer to the question yourself or accept an answer. NEVER include the answer in your question. – mfaani Nov 09 '16 at 15:30
  • 1
    I'm sorry, I changed that – kuzdu Nov 09 '16 at 16:06

1 Answers1

3

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

kuzdu
  • 7,124
  • 1
  • 51
  • 69