在更改应用程序上的初始选项卡栏选择时遇到问题(即在应用程序启动时选择了中间选项卡,而不是最左侧的选项卡)。该应用程序使用 Storyboard ,后来在开发过程中通过 Storyboard 方法添加了选项卡栏 Controller 。
tabBarController.selectedIndex = 1;
上面的代码不起作用(因为我没有将自定义 View Controller 连接到我的标签栏,而只有默认的UITabBarController):
进行了一些谷歌搜索,并查看了许多不同的资源,但尚未找到最初不是使用Apple的模板“Tab Bar应用程序”创建的应用程序的解决方案。
最佳答案
由于这是初始 View Controller ,而不是子类,因此需要在appDelegate中进行设置。
在AppDelegate.m中,将以下内容添加到application:didFinishLaunchingWithOptions:
方法中:
- (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;
}
关于iphone - 如何以编程方式更改初始选项卡栏选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10440884/