使用故事板中的选项卡控制器以编程方式打开视图 [英] Open a view programatically with tab controller from storyboard
问题描述
我有一个故事板,其中包含以下观点(场景):
I have a storyboard which has following views (scenes):
- 登录屏幕
- 带有4个选项卡的TabBarController(库,工作流,设置和下载
根据用户登录,点击按钮(登录),我以编程方式加载标签栏控制器,总是带有以下代码的第一个标签:
Based on user login by clicking on the button (Sign in), I programatically load tab bar controller with always first tab with following code:
LibraryWebViewContoller *lbc = [self.storyboard instantiateViewControllerWithIdentifier:@"docovaMainTabBarController"];
[self presentViewController:lbc animated:YES completion:nil];
我想要做的是能够以编程方式使用TabBarController从storyboard打开不同的选项卡。到目前为止,我能够使用第一个选项卡打开选项卡栏控制器但是没有运气打开其他选项卡,因为它似乎总是加载TabBarController并选择了第一个选项卡。任何建议或提示将不胜感激。
What I want do is be able to open different tabs programmatically with TabBarController from storyboard. So far I am able to open the tab bar controller with first tab but no luck in opening others as it always seem to load the TabBarController with first tab selected. Any suggestions or tips would be appreciated.
推荐答案
我是能够通过使用以下代码解决问题:
I was able to solve the issue by using following code:
UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:@"docovaMainTabBarController"];
tbc.selectedIndex=1;
[self presentViewController:tbc animated:YES completion:nil];
另外,请记住编辑故事板并将UITabBarController的故事板ID设置为docovaMainTabBarController,这样它就可以了在故事板中唯一标识。
Also, remember to edit the storyboard and give the UITabBarController a storyboard ID set to docovaMainTabBarController so it can be uniquely identified within the storyboard.
let tbc = self.storyboard.instantiateViewController(withIdentifier:"docovaMainTabBarController") as! UITabBarController
tbc.selectedIndex = 1
self.present(tbc, animated: true, completion:nil)
这篇关于使用故事板中的选项卡控制器以编程方式打开视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!