0

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

dark_perfect
  • 1,458
  • 1
  • 23
  • 41
  • 1
    Where else is the view being retained? If no where else, then you need to retain it there. – N_A Apr 05 '12 at 14:15

1 Answers1

2

You need to have a retained property (strong | retain) on any topLevel objects from a xib.

enter image description here

In this example above view1 would need to have a retained property. view2 does 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?

view2 does not require a retained property because it is owned by view1 and 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 call self.view2 = nil in viewDidUnload

Community
  • 1
  • 1
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • Ah ha! I did not know this. Based on this, can I also make the assumption that the view controller retains whatever you assign as its view? Many thanks – dark_perfect Apr 05 '12 at 17:33
  • 1
    It's never wise to assume things but I can imagine this would be pretty good guess – Paul.s Apr 05 '12 at 22:24