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

  • SEARCH
  • Home
  • 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 7924011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:31:07+00:00 2026-06-03T17:31:07+00:00

In my viewDidLoad method of my main View Controller I programatically create a segmented

  • 0

In my viewDidLoad method of my main View Controller I programatically create a segmented control and then add it to the titleView of my Navigation Control.

NSArray *seggieItems = [[NSArray alloc] initWithObjects:@"See Entire List",@"See Near Me", nil];
UISegmentedControl *seggie = [[UISegmentedControl alloc]initWithItems:seggieItems];
[seggie setSegmentedControlStyle:UISegmentedControlStyleBar];
[seggie setTintColor:[UIColor redColor]];
[seggie addTarget:self
           action:@selector(seggieChanged:)
 forControlEvents:UIControlEventValueChanged];


self.navigationItem.titleView = seggie;

However when I want to access this Segmented Control from another method, such as viewWillAppear, I can’t – it’s an undeclared identifier.

    - (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [seggie setSelectedSegmentIndex:0]; //this should always be 0 for this view controller viewDidLoad method

}

If I was using a XIB this would be no problem since I’d have an IBOutlet I could use from anywhere. I tried creating a property for my Segmented control, but then the instance variable hides the local variable, so this must be the wrong approach.

What’s the best way to go about this? I apologize if this is a stupid question – I’m fairly new to iOS, currently about halfway through a couple of books. I couldn’t find an answer on Google or searching StackOverflow. I would appreciate any help, and I’m sorry if this is something really simple that I’m missing. Thanks

  • 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-06-03T17:31:08+00:00Added an answer on June 3, 2026 at 5:31 pm

    Making it a property is the correct approach. If you do that, and get the mask warnings you describe, you are probably still declaring the seggie variable in your viewDidLoad method. Remove that declaration and always refer to self.seggie instead of seggie and you’ll be fine.

    By declaration I mean this line:

    UISegmentedControl *seggie = [[UISegmentedControl alloc]initWithItems:seggieItems];    
    

    It should be

    self.seggie = [[UISegmentedControl alloc]initWithItems:seggieItems];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some times when I push ViewController into Navigation Controller, the viewDidLoad() method of the
My main view controller only supports landscapeleft and landscaperight. This viewcontroller in the viewDidLoad
I have the main View controller which adds 2 subviews. - (void)viewDidLoad { [self
there is a method called viewDidLoad that execute a code when the view is
My application has a view controller that extends UITableViewController . The initialization method looks
Many people say during a view controller's viewDidUnload method you must remove subviews by
In my UIViewController's viewDidLoad method I instance a UIImageView instance called stage: stage =
I register a NSNotification in viewDidLoad method. Should I unregister it both in viewDidUnload
I have a simple mapview that has the following viewdidload method and didupdate to
I have created several UI elements in the viewDidLoad method. I want to change

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.