简体   繁体   English

将数据传递到导航控制器中的另一个视图控制器

[英]Pass data to another view controller in navigation controller

I have two view controllers (fitstViewController.swift and secondViewController.swift) in navigation controler. 我在导航控制器中有两个视图控制器(fitstViewController.swift和secondViewController.swift)。

My idea is that In firstViewController there are a button and a textFIeld. 我的想法是在firstViewController中有一个按钮和一个textFIeld。 When I click the button, value in the textField is passed to secondViewController. 当我单击按钮时,textField中的值将传递给secondViewController。

However it makes an error "Thread 1: signal SIGABRT" in the first controller. 但是,它在第​​一个控制器中产生错误“线程1:信号SIGABRT”。

Here is the code in the first controller.. 这是第一个控制器中的代码。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let destination = segue.destinationViewController as! UINavigationController

    let detailController = destination.topViewController as! secondViewController

    detailController.stockSymbol = textField.text
}

I added just one line in the second controller.. 我在第二个控制器中只添加了一行。

    var stockSymbol:String!

How can it be solved? 如何解决?

Thank you in advance! 先感谢您!

In second controller the declared variable should be 在第二个控制器中,声明的变量应为

 var stockSymbol:String?

You should not force unwrap it unless you are sure that it would never be nil. 除非您确定它永远不会为零,否则不要强行打开它。

Replace your old code by: 将旧代码替换为:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    guard let destination = segue.destinationViewController as? SecondViewController else { return } 
    destination.stockSymbol = textField.text
}

暂无
暂无

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

相关问题 如何将数据从视图控制器传递到导航控制器,然后传递到另一个视图控制器? - How to pass data from view controller to a navigation controller and then to another view controller? 如何将数据从一个视图控制器传递到导航控制器中嵌入的另一个视图控制器 - How to pass data from one view controller to another view controller which is embedded in navigation controller 使用导航弹出窗口将数据传递到另一个控制器 - Pass data to another controller using navigation pop 如何将数据传递给嵌入在导航控制器中的集合视图控制器 - How to pass data to collection view controller embedded in navigation controller 将关闭数据传递给导航控制器外部的视图控制器? - Pass data on dismiss to view controller outside of navigation controller? 通过视图控制器,标签栏控制器,导航控制器和视图控制器传递数据 - Pass data through view controller, tab bar controller, navigation controller and to view controller iOS-将数据传递到另一个视图控制器 - iOS - pass data to another view controller 如何将数据传递到另一个视图控制器? - How to pass data to another view controller? 如何使用导航栏项添加操作将已经传递给另一个视图控制器的标签数据传递给其他人? - How to pass label data that is already passed to another view controller using navigation bar item add action? 伪造到另一个视图控制器的导航 - Faking navigation to another view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM