如何以编程方式更改初始选项卡栏选择

时间:2021-11-25 19:42:23

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;

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

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

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.

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

2 个解决方案

#1


28  

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

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

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

在AppDelegate.m中,将以下内容添加到您的应用程序中: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;
}

#2


1  

tabBar setSelectedItem: try using this in your viewDidLoad

tabBar setSelectedItem:尝试在viewDidLoad中使用它

#1


28  

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

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

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

在AppDelegate.m中,将以下内容添加到您的应用程序中: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;
}

#2


1  

tabBar setSelectedItem: try using this in your viewDidLoad

tabBar setSelectedItem:尝试在viewDidLoad中使用它