简体   繁体   中英

Switch to segmented control index within UITabBarController

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?

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.

假设您的主VC中有针对viewControllerA和viewControllerB的@properties,并假定您在viewControllerB中具有@property segmentedControl:

[self.viewContollerB.segmentedControl setSelectedSegmentIndex:1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM