At the moment I am building an iPhone application which has several purposes. One of the purposes a dashboard for known users (need to login). Inside the UINavigationController I use a UIView with a simple button. On the button press I go to either the dashboard page of the login page using a push segue. This works exactly like I intend to do.
But when I am on the login page, after filling the right login credentials, and press the login button. I can go to the dashboard page, but when I press the back button, I'll enter the login page again. Which is not what I want. So I searched for ways to delete the page of the viewControllers array in self.navigationController.viewControllers which should do the job. But this is where I am stuck...
What is the right way to remove a UIViewController from the stack? I tried several things like:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *viewControllersInStack = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
if([viewControllersInStack count] > 3)
{
[viewControllersInStack removeObjectAtIndex:1];
}
//self.navigationController.viewControllers = [[NSArray alloc] initWithArray:viewControllersInStack];
//[self.navigationController setViewControllers:[[NSArray alloc] initWithArray:viewControllersInStack] animated:YES];
}
Putting this in the dashboard class gave me the most success. When pressing the back button in the UINavigationBar, it'll enter the first page but without the UINavigationBar item, but I can press the back button one more time which shouldn't be possible. If I press it, it'll show an empty page with the UINavigationBar item. I have this item for a link to a 'more information' page.
In the login class I tried several things when pressing the login button, some of them are:
//[self.navigationController popViewControllerAnimated:NO];
//[self performSegueWithIdentifier:@"loginToDashboard" sender:self];
//DashBoardViewController *dashboard = [DashBoardViewController alloc];
//[self.navigationController popToViewController:(UIViewController *)dashboard animated:YES];
[self.navigationController popToRootViewControllerAnimated:NO];
[self performSegueWithIdentifier:@"homeToDashboard" sender:self];
The loginToDashboard segue is the one from the login page to the dashboard and the homeToDashboard is from the root to the dashboard. But all of these will give me an exception like:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'homeToDashboard''
or
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'loginToDashboard'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.
Update:
I have the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
//UIBarButtonItem* button = self.navigationItem.backBarButtonItem;
//[button setTitle:@"Test"];
//[button setAction:@selector(returnToHome)];
//self.navigationItem.backBarButtonItem = button;
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"< Home" style:UIBarButtonItemStyleBordered target:self action:@selector(returnToHome)];
self.navigationItem.leftBarButtonItem = backBarButton;
}
- (void)returnToHome
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Now the code works like a charm! But... How can I get the same arrow ('<', less-than sign) as in the default Navigation Controlers.