presentViewController 没有 navigationController swift

presentViewController has no navigationController swift

我到下一个视图的过渡是这样的:

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

这很好用。我想要这样的过渡。但是当我在 MyViewController 中调用以下代码时,NavigationController 为 nil:

if let navigationController = navigationController {
       print("yeah i have a nc")
    } else {
         print("its nil") //this will call
    }

当我使用 navigationController.pushViewController(vc, animated: true) 时一切正常。但我真的很想过渡。这是我这边的错误实现还是 presentViewController 总是没有 navigationController?如果是,我该怎么办?

我的控制器 A 已经嵌入到导航控制器中。我使用 navigationController.presentViewController 转到我的 ViewController。从 MyViewController 我想推到下一个 ViewController C.

对我有用的解决方案

我不知道为什么,但是当您使用 presentViewController 时,您必须为您的 navigationController 定义一个新的(?)根。

在这种情况下,我理解了 Ahmad Fs 的回答。

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

我找到了 "my" 解决方案 here