Why weak IBOutlet NSLayoutConstraint turns to nil when I make it inactive?

Andrey Chernukha picture Andrey Chernukha · Jun 27, 2016 · Viewed 11.5k times · Source

I have an IBOutlet NSLayoutConstraint in my app. Very simple:

@property (nonatomic, weak) IBOutlet NSLayoutConstraint* leftConstraint;

At some point I want to deactivate this constraint:

self.leftConstraint.active = NO;

It happens in a method called from cellForRowAtIndexPath:. And the constraint becomes nil right after the line above. However if I declare this property as strong then it's ok, it doesn't turn to nil. Can anybody explain why it happens?

Answer

Avi picture Avi · Jun 27, 2016

When your outlet is weak, the only strong reference is from the view's constraints property. Deactivating the constraint removes it from that array, so there are no more strong references.