【发布时间】:2016-08-04 08:03:01
【问题描述】:
我有两个视图控制器。
我在第一个视图控制器中有一个测试方法如下
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
myLabel.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func showLabel(){
myLabel.hidden = false
}
}
如果我从另一个班级调用 showLabel,它会给我fatal error: unexpectedly found nil while unwrapping an Optional value
另一个viewController如下
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func Save(sender: AnyObject) {
ViewController().showLabel()
}
}
如果我从 ViewController 调用 showLabel 方法,它可以正常工作,但如果我从 SecondViewController 调用它,则会出错。
【问题讨论】:
-
ViewController()创建一个 new ViewController,它永远不会显示在屏幕上
标签: ios swift uiviewcontroller