3
votes

I'm currently developing an app using a UINavigationController. I set the root view controller to ViewController1 and then push ViewController2 and then ViewController3 in response to button click events.

If I then click the back button from view 3, I'm returned to view 2 but this view has no back button. Interestingly as well, having set titles for each of these views ('View 1', 'View 2' and 'View 3' respectively), if I navigate from view 3 back to view 2 using the back button, the title changes to 'View 1' i.e. the title for the initial view (view 1) - not the title for view 2.

If anyone has any idea what might be going on here, your suggestions are very much appreciated.

Many thanks in advance!

Edit: I use the following code to init the UINavigationController in the app delegate:

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen] bounds]];
self.viewController1 = [[ViewController1 alloc] init];
self.viewController2 = [[ViewController2 alloc] init];
self.viewController3 = [[ViewController3 alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController1];
self.window.rootViewController = self.navigationController;

I later push view controllers to the UINavigationController on button clicks as follows:

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
[self.navigationController pushViewController:appDelegate.viewController2 animated:YES];
1
can you post a bit of code? Also by 'navigate from 3 to 2' do you mean by using the back button? Or are you using some other method? - Damo
Hi Damo - yes, I use the back button to navigate from 3 to 2. I've edited my original post to include the code I use for pushing view controllers. Thanks. - rwbutler
So in the viewController objects do you set a left navigation bar button item - like - self.navigationItem.backBarButtonItem = summat? You say there is a back button but it seems to disappear - is that correct? - Damo
No I let the UINavigationController handle this at present. If I move from the root view controller (viewController1) to viewController2 then a back button is displayed on view 2. If I move from viewController2 to viewController3 then a back button is also displayed on view 3. It's only when I use the back button from view 3 back to view 2 that the back button doesn't show (on view 2). - rwbutler
out of interest - how are the @propertrty declarations for those view controllers looking? Are they definitely retained? (Especially the root one - as thats the one that seems to be getting lost) - Damo

1 Answers

2
votes

I found the solution - in viewController2 and viewController3 I had the following code in order to hide the navigation bar (I wanted the navigation bar hidden on view1 and then visible on views 2 and 3).

- (void) viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillAppear:animated];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillDisappear:animated];
}

I realised it makes far more sense to do the reverse in viewController1 i.e.

- (void) viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}

and then removing the previous code from view controllers 2 and 3. This solved the issue.