更改UITabBar选中的标签页

4

我有一个作为选项卡应用程序创建的应用程序,其中包含3个选项卡(主页、搜索、我的帐户)。主页视图加载一些信息和搜索按钮。搜索按钮然后将用户带到搜索视图,但由于用户没有选择选项卡,所以选定的选项卡仍然是主页选项卡。如何在搜索视图加载后将选定的选项卡从主页更改为搜索?

4个回答

9
感谢您的帮助,你们帮助我找到了正确的方向,最终我使用了App delegate来访问UITabBarController。以下是代码示例:
 Motel_6AppDelegate *appDelegate = (Motel_6AppDelegate*) [[UIApplication sharedApplication] delegate];
    [appDelegate.rootController setSelectedIndex:1];

5

阅读文档。在搜索控制器内部,您可以调用:

self.tabBarController.selectedViewController = self;

UITabBarController 还有一个 selectedIndex 属性,如果这更方便的话。不过,viewDidLoad 可能不是放置此代码的正确位置,因为它可能不会在每次显示搜索控制器时都被调用。相反,您应该直接从在用户点击主屏幕上的搜索按钮时调用的操作内选择选项卡。


3

在您的情况下,使用tabBarController.selectedIndex = intIndex; //其中intIndex为1

将此代码放置在applicationDidLaunch中


0

这段代码在iOS7和Xcode 5的storyboards中对我有效。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接