简体   繁体   中英

Change UITabBar Selected Tab

I have an app that I created as a Tab Bar Application, and there are 3 tabs (Home, Search, My Account). The Home view loads with some info and a search button. The search button then takes the user to the Search View, but since the user didn't select the tab the selected tab is still the Home tab. How do I change the selected tab from the Home to the Search once the Search viewDidLoad?

Thanks for the help, You guys helped me in the right direction I ended up using the App delegate to access the UITabBarController. This is what it looked like.

 Motel_6AppDelegate *appDelegate = (Motel_6AppDelegate*) [[UIApplication sharedApplication] delegate];
    [appDelegate.rootController setSelectedIndex:1];

Read the documentation. From within the search controller, you can call:

self.tabBarController.selectedViewController = self;

UITabBarController also has a selectedIndex property if that's more convenient for you. viewDidLoad is probably the wrong place to put this code in, though, as it might not be called every time the search controller is displayed. You should rather select the tab directly from inside the action that is called when the user taps the search button on the home screen.

Use tabBarController.selectedIndex = intIndex; // inyour case 1

use this code inside applicationDidLaunch

This snippet works for me within storyboards on iOS7 and Xcode 5.

- (IBAction)cancelButtonPressed:(id)sender
{
    [self.tabBarController setSelectedIndex: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