简体   繁体   中英

PopToViewController shows the right ViewController, but the wrong navigationbar

I have an application where the first view is a login screen, and the username and password is used for authentication all around the application. If something go wrong in the authentication process, the user will be thrown back to the login screen, but if the problem occurs when the user press the back button in the navigation bar, the application is showing the login screen correct, but the navigation bar will show the buttons from the view it was supposed to show.

Example: If a user press the back button on View B, and the application is supposed to show View A, but something went wrong, the application will show the login screen, but the navigation bar is the one from View A.

I have tried a couple of things, but the thing that came closest was: In the ViewWillDisappear for View B:

        NSArray * nc = [self.navigationController viewControllers];
    [self.navigationController popToViewController:[nc objectAtIndex:0] animated:YES];

And it gave the result discribed above.

In your LoginController override the viewWillAppear: methods and set the navigation bar button as you want :

-(void) viewWillAppear:(BOOL) animated
{
  [super viewWillAppear:animated];

  //Example :
  self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"AText" style:UIBarButtonItemStyleBordered target:self action:@selector(anAction)];
}

If you don't use ARC remember to add autorelease to the leftBarButton Item :

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"AText" style:UIBarButtonItemStyleBordered target:self action:@selector(anAction)] autorelease];

Remove the login screen after sucessful login [self.window removeFromsuperview] Then it will remove login screen from naviagation. Accept my ans if it happens thank u

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