solerous
solerous

Reputation: 2411

toggling the UINavigationController bar in iOS between segues

I am pretty new to iOS. I have a UIViewController with an embedded UINavigationController and I am hiding the bar on the first View by adding this code to the viewDidLoad method:

if (self) {
    self.navigationController.navigationBarHidden = YES;
    self.navigationItem.title = @"";
}

Then I connected this View to another UIViewController with a seque (triggered by a button on the first View) and added this code to it's viewDidLoad method:

if (self) self.navigationController.navigationBarHidden = NO;

Everything works fine at first. When I load the app the navbar is gone, when I navigate to the second (child) view, the bar is there. Then when I hit the back button, it goes back to the first (parent) view but the NavBar is back.

I tried adding naming the seque "BackToMain" and adding a prepareSeque method which I placed in the second view:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"BackToMain"]) {

        UINavigationController *parentNavigationController = segue.destinationViewController;
        parentNavigationController.navigationController.navigationBarHidden = YES;
    }
}

But it never gets called.

Ideally I would put a method in the parent view to simply hide the NavBar every time it gets displayed. Something like:

- (void)processIncomingSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if (self) self.navigationController.navigationBarHidden = NO;
}

But I really don't know if that is possible.

Upvotes: 1

Views: 173

Answers (1)

Pfitz
Pfitz

Reputation: 7344

Put the following code in your view controller which should not have a UINavigationBar.

- (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];
}

Upvotes: 3

Related Questions