[英]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.