Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a view programmatically with tab controller from storyboard

Tags:

ios

I have a storyboard which has following views (scenes):

  • Login screen
  • TabBarController with 4 tabs ( Library, Workflow, Settings & downloads

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.

like image 269
DP2 Avatar asked Oct 01 '13 23:10

DP2


2 Answers

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.

Swift

  let tbc = self.storyboard.instantiateViewController(withIdentifier:"docovaMainTabBarController") as! UITabBarController
   tbc.selectedIndex = 1
   self.present(tbc, animated: true, completion:nil)
like image 157
DP2 Avatar answered Sep 28 '22 02:09

DP2


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)

like image 32
Kevin Avatar answered Sep 28 '22 02:09

Kevin