Monday, October 3, 2011

How to get rid of an unneeded back button?

Ok, we're trying to debug a kind of strange problem. We're getting a "back" display at the top level of our screen hierarchy.

It has something to do with how the navigation controller is working. They way it works is that the question view controller is called when you press the "Start Quiz" Button. That's done by a push onto the navigation stack. That looks like this:


[self.navigationController pushViewController:questionController animated:YES];



The back button is in effect when you reach the question display, and if you just press the back button right there, before answering any questions, you'll come back to the start display, no problem, no back button.

The problem starts happening when you get all the questions in a quiz right. It will "pop" back into the start controller, with this code:

// return to StartViewController
[self.navigationController popViewControllerAnimated:YES];

Then it will push the "NextLevelViewController" onto the stack, which basically tells you that you've passed the quiz.

It calls it like this:


nextLevelViewController = [[NextLevelViewController alloc] init];

[self.navigationController pushViewController:nextLevelViewController animated:YES];

[nextLevelViewController release];


When you press "Continue" from the NextLevelViewContoller dispaly, it calls a method to pop back to the start controller:


-(IBAction) returnToStart {

NSLog(@"returnToStart method called");
[self.navigationController popViewControllerAnimated:YES];

}


But, when that happens, for some reason the start controller now has the back button active!

Let's see what happens if I don't push to the NextLevelViewController on return from the questions.

It's no problem. So, it's only after it gets back from the next level controller it runs into the problem.

So, what next?

Let's check the documentation:

The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This class is not intended for subclassing. Instead, you use instances of it as-is in situations where you want your application’s user interface to reflect the hierarchical nature of your content. This navigation interface makes it possible to present your data efficiently and also makes it easier for the user to navigate that content.

The screens presented by a navigation interface typically mimic the hierarchical organization of your data. At each level of the hierarchy, you provide an appropriate screen (managed by a custom view controller) to display the content at that level. Figure 1 shows an example of the navigation interface presented by the Settings application in iOS Simulator. The first screen presents the user with the list of applications that contain preferences. Selecting an application reveals individual settings and groups of settings for that application. Selecting a group yields more settings and so on. For all but the root view, the navigation controller provides a back button to allow the user to move back up the hierarchy.


Right. The last line says it all.

A navigation controller object manages the currently displayed screens using the navigation stack. At the bottom of this stack is the root view controller and at the top of the stack is the view controller currently being displayed. You use the methods of your navigation controller object to modify the stack at runtime. The most common operation is to push new view controllers onto the stack using the pushViewController:animated: method. Pushing a new view controller object onto the stack causes the view of that view controller to be displayed and the navigation controls to be updated to reflect the change. You typically push view controllers in response to the user selecting an item that leads to the next level in your information hierarchy.

In addition to pushing view controllers onto the navigation stack, you can also pop them using the popViewControllerAnimated: method. Although you can pop view controllers yourself, the navigation controller also provides a back button (when appropriate) that pops the top view controller in response to user interactions.

A navigation controller object notifies its delegate object in response to changes in the active view controller. The delegate object is a custom object provided by your application that conforms to the UINavigationControllerDelegate protocol. You can use the methods of this protocol to respond to the change and perform additional setup or cleanup tasks.

For more information about how to integrate navigation controllers into your application, see View Controller Programming Guide for iOS.


Ok, what if I display the description of the StartController?

2011-10-03 19:08:40.104 JlptQuizApp[3374:707] ============> StartController, viewWillAppear called =====================>
2011-10-03 19:08:40.108 JlptQuizApp[3374:707] ============> StartController, desc:


Ok. Now, I'm on the start controller *with* the backbutton displaying.

Ok, if I press the back button, nothing happens. I'm going to bag this. It's just not worth it. But it's annoying.

No comments:

Post a Comment