I'm trying to add a button only in code. In the Xcode Simulator it works perfectly but not on my device :
fatal error: unexpectedly found nil while unwrapping an Optional value
My code:
@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)
}
Someone can help me?