Reputation: 625
I have a storyboard which has following views (scenes):
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];
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.
Upvotes: 7
Views: 7891
Reputation: 2555
Swift 5
let storyboard = UIStoryboard(name: "TabBarController", bundle: .main)
let tabbarController = storyboard.instantiateInitialViewController()
tabbarController?.modalPresentationStyle = .fullScreen
self.present(tabbarController!, animated: true, completion: nil)
Upvotes: 0
Reputation: 625
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];
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)
Upvotes: 19
Reputation: 536
Swift4:
let tbc = storyboard.instantiateViewController(withIdentifier: "docovaMainTabBarController") as? UITabBarController
tbc.selectedIndex = 1
present(tbc, animated: true) {() -> Void in }
Upvotes: 0
Reputation: 17536
Say you wanted to have the second view controller selected. Set the activeViewController property on the tab bar controller)
lbc.activeViewController = [lbv.viewControllers objectAtIndex:1];
(not sure of lbc is the tab bar controller or the first view controller in the tab bar - assuming it is)
Upvotes: 1