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

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

tabBarController.selectedIndex = 1; 

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

做了一些Googlesearch,看了很多不同的资源,还没有find一个最初没有使用苹果的模板标签栏应用程序创build的应用程序的解决scheme。

由于这是初始视图控制器,而不是一个子类,你需要在你的appDelegate中设置。

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

tabBar setSelectedItem:尝试在你的viewDidLoad使用这个