我想只在代码中添加一个按钮。在Xcode模拟器它完美,但不是我的设备上:仅在代码中添加按钮?
fatal error: unexpectedly found nil while unwrapping an Optional value
我的代码:
@IBOutlet weak var playButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "playButton.png") as UIImage
playButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
playButton.frame = CGRectMake(10, 10, 100, 100) // crash is here
playButton.center = CGPointMake(self.frame.width/2, self.frame.height/1.7)
playButton.addTarget(self, action: "transition:", forControlEvents: UIControlEvents.TouchUpInside)
playButton .setBackgroundImage(image, forState: UIControlState.Normal)
self.view?.addSubview(playButton)
}
有人能帮助我吗?
这是什么意思,它不适用于您的设备?说它不起作用是无用的。 – Fogmeister 2014-10-08 17:55:46
我无法启动它,我有一个致命错误:“致命错误:在解包可选值时意外发现零” – Haox 2014-10-08 17:57:11
您提供的代码在哪里?这是viewController的'viewDidLoad'方法中的代码吗? – Vinzzz 2014-10-08 18:09:11