如何以编程方式更改初始选项卡栏选择 [英] How to change initial tab bar selection programmatically

查看:107
本文介绍了如何以编程方式更改初始选项卡栏选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更改应用程序上的初始标签栏选择时遇到问题(即,在应用程序启动时选择中间选项卡而不是最左边的选项卡)。该应用程序使用故事板,后期通过故事板方法添加了标签栏控制器。

Having problems changing the initial tab bar selection on an application (i.e. the middle tab is selected on app launch rather than the leftmost tab). The app uses storyboards and the tab bar controller was added later on in development via the storyboard method.

tabBarController.selectedIndex = 1;

以上代码不起作用(因为我没有自定义视图控制器连接到我的标签栏,只是默认的UITabBarController):

the above code doesn't work (because I don't have a custom view controller connected to my tab bar, just the default UITabBarController):

完成了一些谷歌搜索并查看了许多不同的资源,并且还没有找到最初创建的应用程序的解决方案使用Apple的模板标签栏应用程序。

Done some googling and looked at many different resources and haven't quite found a solution for an app that wasn't initially created using Apple's template Tab Bar Application.

推荐答案

由于这是初始视图控制器,并且不是子类,因此需要设置这在您的appDelegate中。

Since this is the initial view controller, and is not a subclass, you need to set this in your appDelegate.

在AppDelegate.m中,将以下内容添加到应用程序中:didFinishLaunchingWithOptions:方法:

In AppDelegate.m, add the following to your application:didFinishLaunchingWithOptions: method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    // Select the left-most tab of our initial tab bar controller:
    UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
    tabBar.selectedIndex = 0;
    return YES;
}

这篇关于如何以编程方式更改初始选项卡栏选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆