
我在storyboard中创建了tabbar.我正在尝试使用以下代码以编程方式更改默认选项卡栏.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"TAB"])
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:@"tabBar"];
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];
}
}
Run Code Online (Sandbox Code Playgroud)
但它没有用.我怎么能这样做?它根本不起作用.
另外,当用户选择第一个标签时,我想回到主视图控制器.我们可以实现此功能.
从当前选定的视图控制器中,您可以使用下面的代码更改选项卡选定的索引。
[self.tabBarController setSelectedIndex:1];
Run Code Online (Sandbox Code Playgroud)
要将选项卡栏控制器添加到视图控制器层次结构,如果您使用导航作为基础,那么您可以使用:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:@"tabBar"];
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];
[self.navigationController pushViewController:tabBar animated:BOOL];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5566 次 |
| 最近记录: |