【发布时间】:2017-06-08 21:31:22
【问题描述】:
我目前正在学习 swift,我正在尝试从情节提要作品中学习实例化,但我现在面临的错误没有太多记录。
我在主情节提要中创建了一个视图控制器,并将其类型指定为我之前调用的自定义类SimpleNewsViewController,这是我的类的代码,它不是复杂:
class SimpleNewsViewController: UIViewController {
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var myText: UITextView!
var event: Events!
override func viewDidLoad() {
super.viewDidLoad()
}
}
在我的主故事板上,这里是我指定的自定义 ViewController: My Storyboard implementation
现在问题来了:在我的代码中,我通过 instanciateViewController(identifier: "NewsView") 实例化了我的 ViewController,然后我尝试像在这段代码中一样设置我的 3 个属性:
...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "NewsView") as! SimpleNewsViewController
//controller.myImage.image = UIImage(named: "image.jpg")
//controller.myText.text = "this is an example that can be really long"
//controller.myTitle.text = "this is a title example
self.navigationController?.pushViewController(controller, animated: true)
...
如果我取消注释这三行,我会收到一条错误消息:
致命错误:在展开可选值时意外发现 nil
IDE 还会显示错误代码和线程(如果有帮助的话): The error
【问题讨论】:
标签: ios swift3 viewcontroller xcode-storyboard