Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios: How open Tab Bar controller from ViewController using StoryBoards

I am doing this and want to open Tab bar Controller from Login Page if its first time and if already login then pass the LoginPage and open Tab Bar Controller after Spalsh

means if user lands in app fiirst time sequesnce should be like this 1. Splash 2. Login Page 3. On successful Login open Tab Bar Controller having 4 tabs

if user already login then 1. Splash 2. Tab bar controller

enter image description here

I am trying to open Tab Bar controller through the following code in ViewDidLoad method

UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:@"TripMapViewer"];
    tbc.selectedIndex=0;
   [self presentViewController:tbc animated:YES completion:nil];

but it gives error

2014-02-06 19:55:43.849 ProjNew[1065:907] -[TripMapViewer setSelectedIndex:]: unrecognized selector sent to instance 0x1d5600b0

and if I remove tbc.selectedIndex=0; it does nothing and stays on Splash screen like this

UITabBarController *lbc = [self.storyboard instantiateViewControllerWithIdentifier:@"TripMapViewer"];
   [self presentViewController:lbc animated:YES completion:nil]; 

please suggest that how do I open tab bar from View Controller

like image 943
Azhar Avatar asked Feb 06 '14 15:02

Azhar


2 Answers

It seems that TripMapViewer is the Storyboard ID of some Tab and not the UITabBarController , please make it sure and it will work

as in the below code MainTabBar is Storyboard ID of UITabBarController and it works perfectly

UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTabBar"];
tbc.selectedIndex=0;
[self presentViewController:tbc animated:YES completion:nil];
like image 91
Azhar Avatar answered Nov 16 '22 00:11

Azhar


You don't need a splash screen controller unless you are animating something. This example uses NSUserDefaults to remember if it's first login or not.

In you application delegate put the following:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   {


        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        if(![[NSUserDefaults standardUserDefaults] dictionaryForKey:@"someKey"]){
            UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"registerViewController"];
            self.window.rootViewController = viewController;
        } else {
            UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"mainViewController"];
            self.window.rootViewController = viewController;
        }

        [self.window makeKeyAndVisible];



         return YES;
  }

If you decide you must have a viewcontroller for the splash screen then you can put the same code there.

like image 41
Mika Avatar answered Nov 15 '22 23:11

Mika