I have a UIScrollView
declared as an iVar in the implementation file of a class that uses ARC:
@interface RecipeBrowserViewController ()
{
UIScrollView *tempScrollView;
}
This is necessary because during execution I need to sometimes add the UIScrollView
to my view, and other times remove that same UIScrollView
:
if (someTest)
{
tempScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
tempScrollView.delegate = self;
[self.view addSubview: tempScrollView];
}
else
{
[tempScrollView removeFromSuperview];
}
My expectation was that the tempScrollView
would be deallocated once it's removed from the superview
, but this is not the case. Presumably, because the iVar is still referencing it?
Adding tempScrollView = nil
after removing it from the superview
clears this up, but I'm not sure what the correct approach is. Am I supposed to declare a weak
iVar instead? Thus far I have only seen weak
suggested for delegates or other two-way iVars that would otherwise lead to a strong reference cycle. Is this another place I should be using it?