简体   繁体   中英

Navigationcontroller is hidden after pushing viewcontroller

I'm working on an app and I face a problem with UINavigationcontroller .

First in the app delegate I check if the user is logged in, if so I take him to Main screen, if notI take him to the login screen.

This is my code:

    UINavigationController *diabetesNavigationController = [UINavigationController alloc];
LoginViewController *loginPage = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
MainViewController *mainPage = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];


if ([DataStore instance].userIsLoggedIn)
    diabetesNavigationController = [diabetesNavigationController initWithRootViewController:mainPage];
else
    diabetesNavigationController = [diabetesNavigationController initWithRootViewController:loginPage];


NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[[UINavigationBar appearance]setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance]setTitleTextAttributes:navbarTitleTextAttributes];

[self.window setRootViewController:diabetesNavigationController];

The problem when the user logs out, he goes back to loginscreen but without the navigationcontroller .

I made something in MainViewController, which is this:

-(void)viewDidAppear:(BOOL)animated
{

    self.navigationItem.title = @"Diabetes";
    UIBarButtonItem *settingButton=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"burger.png"]
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                              action:@selector(onBurger:)];
    self.navigationItem.rightBarButtonItem = settingButton;
    self.navigationItem.leftBarButtonItem.enabled = YES;
}

So when I logout the user, with this code:

LoginViewController *loginPage = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
    [self.navigationController setViewControllers:[NSArray arrayWithObjects:loginPage, nil] animated:YES];

I get to the login screen but without Navigationcontroller . I tried to set Hidden property to NO in all view delegates, but it still has this issue.

You can try this:

UIWindow *window = [UIApplication sharedApplication].keyWindow;
LoginViewController * loginPage = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: loginPage];
[window.rootViewController presentViewController:nav animated:YES completion:NULL];

Let me know if it works.. :)

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