我目前正在得到一个解包可选值时意外发现致命错误在 destViewController.titleLabel.text =“测试 Segue”。
由于当它转向 Seconds ViewController 时出现错误,我该如何修复它?如何避免在 titleLabel.text 处得到 nil ?
class ViewController: UIViewController {
@IBAction func action(sender: AnyObject) {
let alertController: UIAlertController = UIAlertController (title: "Next Page", message: "", preferredStyle: .Alert)
let yesAction = UIAlertAction (title: "YES", style: .Default ) { action -> Void in self.performSegueWithIdentifier("test", sender: self)
}
alertController.addAction (yesAction)
self.presentViewController(alertController, animated: true, completion: nil)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "test") {
let destViewController : SecondViewController = segue.destinationViewController as SecondViewController
destViewController.titleLabel.text = "Testing Segue"
}
}
}
首先更新您的代码ViewController
像这样:
class ViewController: UIViewController {
@IBAction func action(sender: AnyObject) {
let alertController: UIAlertController = UIAlertController (title: "Next Page", message: "", preferredStyle: .Alert)
let yesAction = UIAlertAction (title: "YES", style: .Default ) { action -> Void in self.performSegueWithIdentifier("test", sender: self)
}
alertController.addAction (yesAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "test") {
let destViewController : SecondViewController = segue.destinationViewController as SecondViewController
destViewController.textlbl = "Testing Segue"
}
}
}
之后,我认为您无法用您的方式为文本标签分配值,因此在您的中以这种方式为该标签分配值SecondViewController
:
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
var textlbl = String()
override func viewDidLoad() {
super.viewDidLoad()
self.titleLabel.text = textlbl
}
}
And HERE https://github.com/DharmeshKheni/pass-data-with-Alert我创建了一个示例项目供您参考。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)