Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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
Ios Swift:通过presentViewController传递导航栏_Ios_Swift_Uinavigationcontroller_Uinavigationitem_Presentviewcontroller - Fatal编程技术网

Ios Swift:通过presentViewController传递导航栏

Ios Swift:通过presentViewController传递导航栏,ios,swift,uinavigationcontroller,uinavigationitem,presentviewcontroller,Ios,Swift,Uinavigationcontroller,Uinavigationitem,Presentviewcontroller,在我的故事板中有导航控制器、viewContrl1和viewContrl2。对于下一次更新,我必须设法将viewContrl2转换为viewContrl2的另一个实例 从stackOverflow获得一些帮助后,我通过以下方式进行管理: let newView2 : ViewContrl2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("viewContrl2") as! Vie

在我的故事板中有导航控制器、viewContrl1和viewContrl2。对于下一次更新,我必须设法将viewContrl2转换为viewContrl2的另一个实例

从stackOverflow获得一些帮助后,我通过以下方式进行管理:

let newView2 : ViewContrl2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("viewContrl2") as! ViewContrl2
...
...
view2.presentViewController(newView2, animated: true, completion: nil)
它实际上完全符合我的要求,但顶部没有导航栏。 在ViewContrl2类中,有@IBOutlet弱变量导航:UINavigationItem!,我直接从故事板上连接了它。但是,在新创建的viewContrl2中,导航似乎不起作用。我需要这个,因为用户必须能够按“返回”并返回到以前的视图。有人能解决这个问题吗


谢谢

如果您想看到
导航栏
,请出示
导航控制器
,如下所示:

let newView2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewContrl2") as! ViewContrl2

let navigationControlr = UINavigationController(rootViewController: newView2)

self.presentViewController(navigationControlr, animated: true, completion: nil)
要取消显示的视图控制器,请在视图加载中添加以下代码

//Customise the way you want:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "backAction")
ViewController2
类中添加以下
函数

func backAction(){

    if self.presentingViewController != nil{
        self.dismissViewControllerAnimated(true, completion: nil)
    }else{
        self.navigationController?.popViewControllerAnimated(true)
    }
}

为了能够返回,您需要使用现有的导航控制器。 只需添加一个检查即可确保

let newView2 : ViewContrl2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("viewContrl2") as! ViewContrl2

if let navCtrl = self.navigationController
{
   navCtrl.pushViewController(newView2, animated: true)
}
else
{
   let navCtrl = UINavigationController(rootViewController: newView2)
   self.presentViewController(navCtrl, animated: true, completion: nil)
}

ViewContrl2是UINavigationController的子类?好的,那么。。。我不熟悉故事板,但导航控制器与View1Control的关系是“根视图控制器”,然后我通过View1Controller中的push segue调用View2Control,最后通过presentViewController从View2Controller调用新的View2Control。View1Control和View2Control显示导航,但它甚至不会出现在View2Control显示中,但它似乎与View1Control和View2Control的导航断开连接。它在左上角显示“返回”按钮,但它什么也不做。例如,在view2Control->NewView2Control的情况下,如果用户在newView2Controller中按“返回”,它必须返回view2Controller。从这篇文章中我学到了很多。你的代码一定也能正常工作,但克里斯汀的答案似乎更有效,所以我用了他的。但我真的很感谢你的帮助。非常感谢。不客气:)无论如何,我想你会希望演示文稿保持不变,只需将导航栏转到ViewControllerOkay。。。所以它确实起作用了。然而,在我的newViewController2全部加载后,它立即崩溃,我不知道为什么。。。