Search code examples
iosobjective-cuiviewcontrollerpushviewcontroller

Remove "Back" button after pushing view controller


I have a login screen as my root view controller. If the user is already logged in, I want to skip over the login screen and show the main view controller.

The code below does that, but it shows a back button in the nav bar instead of the default nav bar.

Is there a way to remove the back button? There is a menu button that is supposed to be there, so simply hiding the back button will not suffice.

Thanks!

- (void)viewDidLoad {
    if(GetUserName != nil){
        [self pushingView:YES];
    }
    [super viewDidLoad];
}

-(void)pushingView:(BOOL)animation{
    MainViewController *revealViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"RevealViewController"];
    [self.navigationController pushViewController:revealViewController animated:animation];
}

Solution

  • Don't try to remove the back button, instead remove the need for the back button. Instead of using pushViewController:animated: use setViewControllers:animated:. In this way you only have one view controller on the stack (and you save the memory of keeping the view controller in existence).

    Ideally you wouldn't even load the login view controller and then you'd save even more memory and time.