我想只在代碼中添加一個按鈕。在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