0

In my application, i have a navigation controller. I have added a back button to go to the login screen (UIViewController). How do i go back to the login screen from the navigation controller?

This is the code for my back button.

(void)viewDidLoad
{

    [super viewDidLoad];

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemCancel target:self action:@selector(goBacktoLogin)];

    self.navigationItem.leftBarButtonItem = backButton;

}

(void) goBacktoLogin

{

    LoginViewController *loginViewController = [[ LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];

[[ self navigationController] popToViewController:loginViewController animated:YES];


}

I am getting the following error:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException',

reason: 'Tried to pop to a view controller that doesn't exist.'

Bazinga
  • 2,456
  • 33
  • 76
Amrita
  • 65
  • 1
  • 3
  • 9

2 Answers2

1

If you need to go back to the very first view controller in your navigation hierarchy, then use:

[self.navigationController popToRootViewControllerAnimated:YES];
sergio
  • 68,819
  • 11
  • 102
  • 123
0

Your question is not clear to me. But I think this could help:

Push login controller in to the navigation stack using this,

LoginViewController *loginViewController = [[ LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];

self.navigationController pushViewController:loginViewController animated:NO];

and in the pushed screen(login view controller):

push another controller. Then add a back button. on click of it

execute this command:

[self.navigationController popViewControllerAnimated:YES];
Jacob
  • 2,041
  • 14
  • 16