简体   繁体   中英

Open a view programmatically with tab controller from storyboard

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.

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)

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)

斯威夫特4:

let tbc = storyboard.instantiateViewController(withIdentifier: "docovaMainTabBarController") as? UITabBarController tbc.selectedIndex = 1 present(tbc, animated: true) {() -> Void in }

Swift 5

let storyboard = UIStoryboard(name: "TabBarController", bundle: .main)
let tabbarController = storyboard.instantiateInitialViewController()
tabbarController?.modalPresentationStyle = .fullScreen
self.present(tabbarController!, animated: true, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM