Giovanni Polì
Giovanni Polì

Reputation: 33

viewControllers that doesn't appear

I have a question for you: I have a set of UIViewController attached to self.tabBarController.viewControllers and i have another one alone that is supposed to be the login scree that appear just once (the first time you open the app ever), and i wish to load that one in case the user is not logged in, otherwise or after that the user log in, it will load the full self.tabBarController.viewControllers that i have. Here is the code:

-(void)load_login_view{
    NSLog(@"map");
    UIViewController * fb_login = [[FacebookLoginView alloc]init];
    fb_login.title = @"fsf ss";
    UINavigationController * fb_login_navigation = [[UINavigationController alloc] initWithRootViewController:fb_login];
    [fb_login_navigation.tabBarItem setImage:[UIImage imageNamed:@"eventi.png"]];
}



- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL login_status = [defaults objectForKey:@"login_status"];


    UIViewController * secondpage = [[SecondViewController alloc]init];
    secondpage.title = @"second";
    UINavigationController * secondpage_navigation = [[UINavigationController alloc] initWithRootViewController:secondpage];
    [secondpage_navigation.tabBarItem setImage:[UIImage imageNamed:@"eventi.png"]];



    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[secondpage_navigation];
    self.window.rootViewController = self.tabBarController;


    if(!login_status){
        [self load_login_view];
    }else{

    }

    [self.window makeKeyAndVisible];

}

Upvotes: 0

Views: 38

Answers (1)

jregnauld
jregnauld

Reputation: 1298

You have many ways to do that. There is a couple of examples.

  1. With your current navigation flow

-> if the user is not logged, you can display your login view controller as modal so it will be over top of your tabBarController. //something like that

    -(void)load_login_view{
    UIViewController * fb_login = [[FacebookLoginView alloc]init];
                fb_login.title = @"fsf ss";

    UINavigationController * fb_login_navigation = [[UINavigationController alloc] initWithRootViewController:fb_login];

    [self.window.rootViewController presentViewController:fb_login_navigation animated:NO completion:nil];

    }

->when you user is logged, dismiss the login controller. If needed, save user data for the next time

->do some work to update the selected controller inside your TabBarcontroller if needed.

  1. Change your navigation flow

-> use UINavigationController with the login Controller ([[UINavigationController alloc] initWithRootViewController:fb_login]) as a rootController for your application

UIViewController * fb_login = [[FacebookLoginView alloc]init];
fb_login.title = @"fsf ss";
UINavigationController * fb_login_navigation = [[UINavigationController alloc] initWithRootViewController:fb_login];
self.window.rootViewController  = fb_login_navigation;

-> when you user is logged, push to your TabBarcontroller. If needed, save user data for the next time

//inside fb_login controller ( you can optimize the code, it's just a quick example)

UIViewController * secondpage = [[SecondViewController alloc]init];
secondpage.title = @"second";
UINavigationController * secondpage_navigation = [[UINavigationController alloc] initWithRootViewController:secondpage];
[secondpage_navigation.tabBarItem setImage:[UIImage imageNamed:@"eventi.png"]];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[secondpage_navigation];
[self.navigationController pushViewController:tabBarController animated:YES];

->To avoid the double navigationBar (fb_login_navigation / secondpage_navigation), you can hide the navigation of fb_login_navigation when needed.

-> the next time, if the user is logged, you can fire the code above just after loading the login controller instead of waiting that the user enters his credentials.

Hope it helps.

Upvotes: 1

Related Questions