0
votes

I have a UITabBarController with 2 tabs, call them A and B. B has a UISegmentedControl within it's view controller. When I am in tab A, how can I programmatically switch to tab B and to a certain index in the segmented control?

I know I can do this to switch tabs:

self.tabBarController.selectedIndex = 1;

but how do I switch to a certain index in the segmented control?

2

2 Answers

1
votes

The proper approach for this is for View Controller A (VCA) to send a message to the app delegate which in turn sends a message to VCB. Neither of these should know anything about the segmented control. When the method on VCB is called, the view controller should then have the knowledge to update its view(s) as needed.

VCA and VCB should not know about each other and certainly should not have any access to each other's views.

Another option is to have some common data model used by both view controllers. Then when VCA updates the model, VCB can react to the change by updating itself as needed. Then neither VC needs to send a message to the app delegate.

0
votes

Assuming you have @properties in your master VC for viewControllerA and viewControllerB, and assuming you have a @property segmentedControl in your viewControllerB:

[self.viewContollerB.segmentedControl setSelectedSegmentIndex:1]