I have a view in a nib, which is linked to a property in my viewcontroller with the following line:
@property (unsafe_unretained, nonatomic) IBOutlet UIView *otherView;
It is unsafe_unretained because we are targeting ios 4 devices, but using ARC.
We are getting a crash because the otherView is being deallocated when we are trying to show it, and I’m not too sure why. I’ve put a breakpoint in viewWillAppear, and if I do “po otherView” in the debugger, i get:
<UIView: 0x6fcc880; frame = (0 0; 320 460); autoresize = RM+BM; layer = <CALayer: 0x6fcc8b0>>
I checked it at the end of the viewWillAppear method, and it is still there too. But then if I put a breakpoint at the beginning of viewDidAppear, I get:
0x6fcc880 does not appear to point to a valid object.
Can anyone point me in the right direction with this? If I change the property declaration to ‘Strong’, then this issue doesn’t occur, and I understand that by changing it to Strong that I am retaining it (and therefore preventing it from being deallocated), but I don’t think I should need to do this?
Regards,
Nick
You need to have a retained property (
strong|retain) on any topLevel objects from a xib.In this example above
view1would need to have a retained property.view2does not require a retained property but I generally just leave it as retained anyway as it does not hurt anything.Why is the retain not required?
view2does not require a retained property because it is owned byview1and any references you haveare arbitrary references between objects that do not imply ownership.(Apple, Resource Programming Guide). But it doesn’t hurt to have a retained property either, just make sure to callself.view2 = nilinviewDidUnload