views:

92

answers:

1

In am currently creating a Navigation-based application for the iPhone, where the root view of the NavigationController is the main menu (home). There are 3 other views (named View1, View2 and View3), which can be accessed in this manner; main menu -> View1 -> View2 -> View3.

Each of the Views has a 'Home button' that will call 'popToRootViewControllerAnimated' and return to the main menu. When this button is pressed in View2, it returns to the main menu. However, when you go from the main menu to View1, the Back button will be missing. This is the only View that suffers from the problem.

The thing that has brought this around is that we are 'lazily initialising' View1. In other words, we are keeping a reference to View1 in the main menu so that we have it constantly at hand (to keep the state of that view).

The weird thing is that this problem only happens by pressing the 'Home button' from View2. With View3 there is no issue.

In each of the views' 'ViewWillAppear' methods, we have the following code that will add a back button to the view's NavigationBar;

[self.navigationItem.backBarButtonItem release];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

Does anyone have any idea on how to remedy this problem?

+1  A: 

You must apply this code in the previous view controller, as the back button depends on the view controller above the visible view controller in the navigation stack.

jrtc27
I have done this - all of the views (including the main menu) has the code above.
Mabbage
Why are you releasing the backBarButtonItem? You don't own it.
jrtc27
I believe that was from taking code from another question on stackoverflow about customising the back button. With or without that line, it makes no difference. I have since commented out anything that will alter the back button or navigation bar and the problem still occurs.
Mabbage
Can you please post your code for how you show the view controllers? Thanks
jrtc27