Andrey Chernukha Andrey Chernukha - 5 months ago 90
iOS Question

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

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?

Avi Avi
Answer

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.