Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift presentViewController没有navigationController_Swift_Navigationcontroller_Presentviewcontroller - Fatal编程技术网

Swift presentViewController没有navigationController

Swift presentViewController没有navigationController,swift,navigationcontroller,presentviewcontroller,Swift,Navigationcontroller,Presentviewcontroller,我向下一个视图的转换如下所示: if let navigationController = navigationController { if let storyboard:UIStoryboard = UIStoryboard(name: "myStoryboard", bundle: nil) { if let vc = storyboard.instantiateViewControllerWithIdentifier("myViewControl

我向下一个视图的转换如下所示:

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,动画:true) 一切正常。但我真的很想过渡。这是我的错误实现还是
presentViewController
总是没有导航控制器?如果是,我能做什么


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

解决方案,该解决方案适合我

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

在这种情况下,我理解了艾哈迈德的回答

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

我找到了“我的”解决方案

是否要显示与navigationController连接的viewController?所以,在演示之后,你可以推动和弹出。。。是这样吗?是的,是这样。我的控制器A已嵌入navigationController中。我使用navigationController.presentViewController获取MyViewController。从MyViewController我想推到下一个ViewControllerSo,我假设如果您试图从显示的控制器导航到另一个viewController,它将显示而不是推,对吗?你说“它将显示而不是推”是什么意思?当我用推的时候,一切都很好。我在下一个VC中有一个navigationController。当我使用presentViewController时,我没有。对不起,我更改了它