如何在不使用segue的情况下将某个值从一个视图控制器传递到swift 4中的UINavigationController?

时间:2018-03-22 06:41:15

标签: ios swift3 swift4 xcode9

我想在不使用swift 4中的segue的情况下将值从一个UIviewController传递到UINavigationViewController。

我的示例代码如下

let vc = storyboard?.instantiateViewController(withIdentifier: "MainMenuId") as! UINavigationViewController
vc.sendValue = String    //String send to sendValue which present in UINavigationController           
self.present(vc!, animated: true, completion: nil)

但是当我尝试发送时,我的应用程序崩溃并收到错误

无法将'UINavigationController'类型的值转换为'UIViewController'

那么如何在不使用segue的情况下从上面的结构中传递值。 Thx提前;)

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

let vc = storyboard?.instantiateViewController(withIdentifier:"MainMenuId") as! UIViewController
let nav = UINavigationController(rootViewController: vc)

vc.sendValue = String              
self.present(nav!, animated: true, completion: nil)