Search code examples
iosobjective-cuinavigationcontroller

Push to ViewController without back button


I am developing an iOS app which contains login/authentication functionality - basically first time a user logins, in they need to enter relevant login details - then they are passed to main app screens - subsequent visits to the app they will be automatically authenticated.

All above works fine - the issue I have is with the Navigation bar - it appears in the main screen in the main part of the app with a back button - I don't want this to be displayed as they should not be able to return to the login screen once authenticated. I guess it's using the root navigation controller which explains the logic, but is there a way to ignore the navigation controller of the login section so the back button is not displayed in the main app.

Below is a screenshot of the structure to help my explanation - left hand group of screens are the login process right hand is the main app structure.

enter image description here

The code used to switch screens is as follows -

SWRevealViewController *swRevealController = (SWRevealViewController *)navVC;
swRevealController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:controller animated:YES];

Solution

  • In the Screen which implements after login, the ViewDidLoad method add the line to hide back bar button.

    self.navigationItem.hidesBackButton = YES;
    

    Additionally you can add an 'Logout' option as

    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStyleBordered
                                                                     target:self
                                                                     action:@selector(LogoutClick)];
    self.navigationItem.leftBarButtonItem = backBarButton;
    
    -(void)LogoutClick {
        [self showLogoutAlert];
    }
    
    -(void)showLogoutAlert {
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:@"Do you want to Logout ?"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Logout", nil];
        [alert show];
    }
    
    
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
        if (buttonIndex == 1) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }