I'm instantiating a shop via a xib:
let cShop = UINib(nibName: "connectedShop", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! connectedShop
This shop has assigned this custom class:
import Foundation
import UIKit
class connectedShop : UIView{
@IBOutlet weak var bannerViewHight: NSLayoutConstraint!
@IBOutlet weak var bannerViewTop: NSLayoutConstraint!
@IBOutlet weak var bannerViewLeft: NSLayoutConstraint!
@IBOutlet weak var bannerViewRight: NSLayoutConstraint!
@IBOutlet weak var banerDiscount: UIImageView!
@IBOutlet weak var bannerImageShop: UIImageView!
@IBOutlet weak var bannerTitle: UILabel!
@IBOutlet weak var bannerDescription: UILabel!
@IBOutlet weak var bannerButton: UIButton!
@IBAction func bannerButtonAction(sender: UIButton) {
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
}
And from the code I'm calling this shop like this:
let cShop = UINib(nibName: "connectedShop", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! connectedShop
cShop.bannerTitle.text = "shopTitle" //work
shopContainer.addSubview(cShop)
cShop.bannerViewLeft.constant = 0 //make my app crash
cShop.bannerViewRight.constant = 0 // "
cShop.bannerViewTop.constant = 0 // "
When I set the bannerTitle.text
property my app continues and debugging I see this outlet is initialized, but the constraints aren't and when the compiler are in the line that assign a constant, it always throw the same error:
fatal error: unexpectedly found nil while unwrapping an Optional value
debugging I've seen there is one outlet that is initialized:
Does anybody know why this can be happening?
These are my conections:
I had the same issue. Change your constraint outlets to strong.
How to make constraint outlets Strong: Just remove weak from declaration.
@IBOutlet weak var bannerViewHight: NSLayoutConstraint!
to
@IBOutlet var bannerViewHight: NSLayoutConstraint!
Do it for all and thats it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With