简体   繁体   English

更改UITabBar选定的选项卡

[英]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). 我有一个我作为标签栏应用程序创建的应用程序,有3个选项卡(主页,搜索,我的帐户)。 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? 一旦搜索viewDidLoad,如何将所选选项卡从主页更改为搜索?

Thanks for the help, You guys helped me in the right direction I ended up using the App delegate to access the UITabBarController. 感谢您的帮助,您们帮助我朝着正确的方向前进,我最终使用App委托来访问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. 如果对你来说更方便, UITabBarController也有一个selectedIndex属性。 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. viewDidLoad可能是放入此代码的错误位置,因为每次显示搜索控制器时都可能不会调用它。 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; 使用tabBarController.selectedIndex = intIndex; // inyour case 1 //在你的情况下1

use this code inside applicationDidLaunch 在applicationDidLaunch中使用此代码

This snippet works for me within storyboards on iOS7 and Xcode 5. 这个代码片段适用于iOS7和Xcode 5的故事板。

- (IBAction)cancelButtonPressed:(id)sender
{
    [self.tabBarController setSelectedIndex:1];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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