1
votes

I have a simple question regarding ARC. I show a UIView if a user taps a button using addSuperView within a UIViewController. The UIView contains a close button, if tapped I want to remove the view.

I used to call a method within the UIViewController after animating the view offscreen:

- (void)viewDidClose:(UIView *)view
{
     [view removeFromSuperview];
     [view release], view = nil;
}

Now using ARC I changed it to:

- (void)viewDidClose:(UIView *)view
{
     [view removeFromSuperview];
     view = nil;
}

The question now is: I want to remove the protocol and the delegation to the view controller and do this within the UIView itself.

Pre-ARC (within view):

- (void)didStop
{
     [self removeFromSuperview];
     [self autorelease];
}

I can't use 'autorelease' in ARC nor set 'self = nil', as far as I know ARC comes in place as soon as I set the view to nil or replace it, but what if I don't replace it? Is [view removeFromSuperview] enough to take care of everything or does this leak?

Thanks a lot! I appreciate any help!

1

1 Answers

0
votes

(Note that in your non-ARC version, you'd want the autorelease before the removeFromSuperview).

I've wondered about similar things. The 'danger' is that when you do the removeFromSuperview without first doing an autorelease, the object is immediately deallocated, even before the end of the method. That's skeezy.

My idea was that you'd make the method return self. In the non-ARC case, you'd create this pointer with autorelease but ARC should do that for you. You may not need the pointer in the caller, but I believe this will force ARC to do what you want.