Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLayoutConstraint outlets are allways nil while other outlets aren't

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:

enter image description here

Does anybody know why this can be happening?

These are my conections:

enter image description here

like image 495
Alfro Avatar asked Jul 20 '16 07:07

Alfro


2 Answers

I had the same issue. Change your constraint outlets to strong.

like image 159
SergioM Avatar answered Sep 24 '22 20:09

SergioM


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.

like image 26
Naveen Kumawat Avatar answered Sep 22 '22 20:09

Naveen Kumawat