简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM