[英]UILabel is nil on runtime, works on simulator, but crashes on device
我正在学习Swift 2,并将旧的Objective-C代码更改为Swift。
我只是将UILabel放在笔尖中并将其连接到笔尖的Controller。
该代码可在模拟器(iOS 8.4和iOS 9)上运行,但在设备(iPhone 4s和iPhone 5s,均为iOS 8.4)上崩溃,崩溃信息为: fatal error: unexpectedly found nil while unwrapping an Optional value
。
以下是相关代码:
我的视图控制器中的代码:
var round: Int = 0
@IBOutlet weak var nextRoundLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.nextRoundLabel.text = "\(self.round)"
}
}
推送viewController的代码:
let rvc = RoundViewController()
rvc.modalTransitionStyle = UIModalTransitionStyle.PartialCurl
rvc.round = 1
self.navigationController?.pushViewController(rvc, animated: true)
发生错误的实例:
我在iOS8.4上遇到了同样的问题。 当按下视图控制器时,应指定笔尖名称。 为此,请在RoundViewController中覆盖init。
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
并实例化控制器:
let rvc = RoundViewController(nibName: "RoundViewController")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.