I’m building a segmented control within my viewDidLoad method, like so:
NSArray *tabitems = [NSArray arrayWithObjects:@"ONE", @"TWO", nil];
UISegmentedControl *tabs = [[UISegmentedControl alloc] initWithItems:tabitems];
tabs.segmentedControlStyle = UISegmentedControlStyleBar;
tabs.frame = CGRectMake(185.0, 7.0, 130.0, 30.0);
tabs.selectedSegmentIndex = 0;
[self.navigationController.navigationBar addSubview:tabs];
[tabs release];
But when the user goes Back in the uinavigationcontroller hierarchy, the segmented controller stays on the navigation bar. How would I get rid of it? Or am I doing something fundamentally wrong?
EDIT
Following Alex’s suggestions, I propertized tabs and tried:
NSArray *tabItems = [NSArray arrayWithObjects:@"FAQs", @"Terms", nil];
self.tabs = [[UISegmentedControl alloc] initWithItems:tabItems];
but I’m not sure it’s a good idea to alloc a property;
And I’m using
[self.tabs removeFromSuperview];
in my viewWillDisappear. Is that enough?
Retain a reference to the segmented control in your view controller (i.e define
tabsas a property in the view controller’s header file).Override the view controller’s
-viewWillDisappear:method, and remove the segmented control from the navigation bar there, using the control’s-removeFromSuperviewmethod.EDIT
You would still
alloc-inityour segmented controltabsin-viewDidLoad. You just need to set up a retain property fortabsin your view controller’s header, and move the control’sreleasestatement to the view controller’sdeallocmethod.Read the “Properties” section of this Objective-C tutorial for an introduction to properties and how to set them up.
The way to override a method is as follows:
In the case of
-viewWillDisappear:, this method gets called when your view controller is about to disappear, such as when it gets popped off the navigation stack. This is a great place to put code that manages clean-up of view-controller-specific items, like your segmented control:EDIT 2
If your property is set as follows:
then you are going to
retainanything you setself.tabsequal to.Your code here:
will create a memory leak, because you are retaining this object:
[[UISegmentedControl alloc] init]— but you never release[[UISegmentedControl alloc] init]itself. This is bad.Instead, use
autoreleaseon the right side, i.e.:The
tabsproperty retains its own reference to the initialized segmented control. That initialized segmented control is itself released properly at some later point. So no more memory leak.