Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 785101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:49:15+00:00 2026-05-14T20:49:15+00:00

I’m building a segmented control within my viewDidLoad method, like so: NSArray *tabitems =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:49:16+00:00Added an answer on May 14, 2026 at 8:49 pm

    Retain a reference to the segmented control in your view controller (i.e define tabs as 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 -removeFromSuperview method.

    EDIT

    You would still alloc-init your segmented control tabs in -viewDidLoad. You just need to set up a retain property for tabs in your view controller’s header, and move the control’s release statement to the view controller’s dealloc method.

    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:

    - (ReturnClass) methodNameToOverride:args {
        [super methodNameToOverride:args];
        // your code goes here...
    }
    

    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:

    - (void) viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [tabs removeFromSuperview];
    }
    

    EDIT 2

    If your property is set as follows:

    @property (nonatomic, retain) UISegmentedControl *tabs;
    

    then you are going to retain anything you set self.tabs equal to.

    Your code here:

    self.tabs = [[UISegmentedControl alloc] initWithItems:...];
    

    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 autorelease on the right side, i.e.:

    self.tabs = [[[UISegmentedControl alloc] initWithItems:...] autorelease];
    

    The tabs property 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A simple way would be to just loop: var caretPos… May 15, 2026 at 3:54 pm
  • Editorial Team
    Editorial Team added an answer A transformation matrix is a factor of scaling matrix Ss,… May 15, 2026 at 3:54 pm
  • Editorial Team
    Editorial Team added an answer If your only concern is encrypting the database connection strings,… May 15, 2026 at 3:54 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.