Maximus S
Maximus S

Reputation: 11095

Disable going back to previous view controller from navigation controller

Setting:

After you sign up from SignUpViewController you go to a PostsViewController. They are connected via a show segue, so inside the navigation bar, < SignUp button is automatically created.

Problem:

I want to hide this button because you would never have to go back to SignUpViewController unless you've logged out. I thought removing the view controller from the navigation stack would do the job. I did the following:

NSArray *navVCs = [self.navigationController viewControllers];
for (UIViewController *vc in navVCs) {
    if ([vc isKindOfClass:[SignUpViewController class]]) {
        [vc removeFromParentViewController];
    }
}

I checked that SignUpViewController was correctly removed from the navigation stack, but it still doesn't remove the back button.

I also tried setting

self.navigationItem.hidesBackButton = YES;
self.navigationController.navigationBar.userInteractionEnabled = NO;
self.navigationItem.backBarButtonItem = nil;
self.navigationItem.leftBarButtonItem = nil;

and none of them remove the < SignUp button from the navigation bar inside PostsViewController.

Solution:

I've looked at everywhere on StackOverflow and I am surprised I couldn't find an answer. Thank you in advance for your help!

Upvotes: 1

Views: 660

Answers (1)

Gordonium
Gordonium

Reputation: 3487

Rather than disabling the back button I would recommend reviewing the design sequence of the view controllers.

Instead of a transition like this:

NavController -> SignUp -> Posts

Try something like this

Signup || NavController > Posts

Where the Signup view is no longer connected via a segue to the nav controller.

You could detect on app start if the signup screen is necessary and if it is present it modally (or however you wish). When the user has finished signing up you can then instantiate the Posts controller, which is embedded in a nav controller, rather than segueing to it. This way there will be no previous Signup view to return to and no back button.

Upvotes: 3

Related Questions