War*_*ior 19 iphone uitabbar default-selected
我是iPhone开发的新手.我正在创建基于视图的应用程序.我在视图中添加了一个标签栏(而不是标签栏控制器).通过将标签栏项目的标签值设置为1,2,我已在tabbar项目单击事件上加载了每个标签栏的视图.
我希望默认选中标签栏1.我该怎么办?
这是我的代码:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
[self activateTab:item.tag];
}
- (void)activateTab:(int)index {
switch (index) {
case 1:
self.tab1ViewController =[[tab1 alloc] initWithNibName:@"tab1" bundle:nil];
[self.view insertSubview:tab1ViewController.view belowSubview:tabbar1];
if (currentViewController != nil)
[currentViewController.view removeFromSuperview];
currentViewController = tab1ViewController;
break;
case 2:
self.tab2ViewController =[[tab2 alloc] initWithNibName:@"tab2" bundle:nil];
[self.view insertSubview:tab2ViewController.view belowSubview:tabbar1];
if (currentViewController != nil)
[currentViewController.view removeFromSuperview];
currentViewController = tab2ViewController;
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我在界面构建器中添加了标签栏.我可以在界面构建器中执行任何操作吗?
小智 36
[tabBar setSelectedItem:[tabBar.items objectAtIndex:item.tag]];
Run Code Online (Sandbox Code Playgroud)
Mat*_*at0 13
对于swift 3.0,如果@IBOutlet在viewDidLoad中使用tabBar :
tabBar.selectedItem = tabBar.items?.first
Run Code Online (Sandbox Code Playgroud)
phe*_*cks 10
您是否只能在显示视图时调用方法选择选项卡?像这样:
[self activateTab:1];
Run Code Online (Sandbox Code Playgroud)
要更改选择了哪个标签栏项,请使用:
[myTabBar setSelectedItem:myTabBarItem];
Run Code Online (Sandbox Code Playgroud)
其中myTabBarItem是相关视图的UITabBarItem实例.
您可以通过设置selectedIndex属性来设置TabBarController的默认索引.这可以放在viewDidLoad中或者在推送控制器之前如果你这样做的话.仅当您使用TabBarController而不仅仅是TabBar时才会执行此操作.
tabBarController.selectedIndex = 1;
Run Code Online (Sandbox Code Playgroud)
如果您使用的是没有TabBarController的TabBar,那么您必须这样做.
self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:1];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66169 次 |
| 最近记录: |