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
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.seggieinstead ofseggieand you’ll be fine.By declaration I mean this line:
It should be