samedi 27 juin 2015

Unexpectedly found nil while unwrapping an Optional value (UIButton)

I'm trying to create a button programmatically and add it to a UIScrollView in Swift. The code below is what I am using and throws the infamous "unwrapping optional error" where I add the button as a subview. Why is it doing this? I have checked and compared my code to others' and all seems to look fine.

let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(8, 2, 200, 300)
button.backgroundColor = UIColor.grayColor()
button.setTitle(productName, forState: UIControlState.Normal)
self.productButtonView.addSubview(button) //This line throws error

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire