简体   繁体   English

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

[英]How do I pass some value from one view controller to UINavigationController in swift 4 without using segue?

I want to pass value from one UIviewController to UINavigationViewController without using segue in swift 4. 我想将值从一个UIviewController传递给UINavigationViewController而不在swift 4中使用segue。

My sample code is following 我的示例代码如下

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)

But when I try to send at at that time my app is crashed and get error 但是当我当时尝试发送时,我的应用程序崩溃了,并报错

Could not cast value of type 'UINavigationController' to 'UIViewController' 无法将类型“ UINavigationController”的值转换为“ UIViewController”

So How do I pass value from above structure without using segue. 因此,如何不使用segue从上述结构传递值。 Thx in advance ;) 提前谢谢;)

You can use below code: 您可以使用以下代码:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从嵌入在 UINavigationController 中的一个视图控制器转移到另一个 UINavigationController - Segue from one view controller embedded within UINavigationController to another UINavigationController 如何将引用从调用视图控制器传递给呈现有segue的引用? - How do I pass a reference from a calling view controller to one presented with a segue? 我正在尝试通过“准备转场”将数据从一个视图控制器传递到另一个视图控制器,但它崩溃了 - I am trying to pass data from one View Controller to another in Swift via 'prepare for segue' but it crashes 无法使用 segue swift 5 将数据从一个 controller 传递到另一个 - Not able to pass data from one controller to another using segue swift 5 如何从UINavigationController选择到UIViewController - How do I segue from a UINavigationController to a UIViewController 如何将值从一个视图控制器传递给另一个swift? - How to pass value from one view controller to another swift? 在不使用Segue的情况下将数据从一个视图控制器传递到另一个 - Passing data from one view controller to another without using Segue 使用Swift,如何从另一个视图控制器中的按钮调用在一个视图控制器中编码的函数? - Using Swift, how do I call a function coded in one view controller from a button in another view controller? 如何在标签栏控制器中从一个视图控制器切换到另一个视图控制器并保留标签栏? - How within a tab bar controller do I segue from one view controller to another and retain the tab bar? 如何将图像视图从视图控制器传递到选项卡式视图控制器而无需保持链接 - How to pass image view from view controller to tab bar view controller without segue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM