0

I am not able to select the index in my UISegmentController. I can manually touch via the simulator and I can set the selectedSegmentIndex via code, but I am not able to have it show up in the simulator the index that I have set via code. What I would like to see is that the index that I have set shows as being selected to the user, without the user actually physically clicking on the item.

 if([mealPlanTable isHidden]){
     segCont.selectedSegmentIndex = 0;
     NSLog(@"%d", [segCont selectedSegmentIndex]);
     [self mainControl:segCont];
 }

This will set the index and then call the mainController which does an action based on the index selected, which does work. I am able to use the value that I set but am just not able to visually see that the Segment is selected aka is blue in the simulator.

2 Answers 2

2

How is the segmented control added to the view? If it is via interface builder, make sure you linked it as an IBOutlet and use it's property for access ie self.segmentedControl. If you added it directly, again, make sure you are using the proper instance variable. Also make sure you are calling it from the proper place in view loading.

Sign up to request clarification or add additional context in comments.

4 Comments

I create a UISegmentControl *segCont
Sorry. I create that in the .h then create an IBOutlet. I am calling it via segCont.selectedSegmentIndex
If you have connected the outlet try using the property accessor - self.segCont.selectedSegmentIndex = 0
I tried that after you mentioned it and it did not change anything.
2

Where are you calling this? If it's in init, try placing it in the viewDidLoad or viewWillAppear method.

7 Comments

I am calling it in another function that does another action, but changes my view. I have a search bar that fires and when it fires I want to show the results on one of the views that may be different from which segmentedControl is on. With that I am trying to change the selectedIndex manually so that I can then update the view by calling the IBAction that I have attached to the Interface for when the UISegmentedController changes.
It sounds like a bad IB link then, as shawnwall suggested. If it's done after viewDidLoad. What does the NSLog above show?
It shows the selectedSegmentIndex that I just set, which is actually the correct index that I wanted to set. So if I set it to 0 like above it shows 0, but doesn't update visually.
Nope not bad IB just bad programmer lol. I linked the Action but not the outlet because I built it after and thought I did. The outlet works now. Thank you both.
shawnwall called the IBOutlet first, he should get the correct answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.