Dancer
Dancer

Reputation: 17651

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

Upvotes: 16

Views: 22092

Answers (8)

Fattie
Fattie

Reputation: 12582

2022 answer

@IBAction func SomeScreen() {
    let s = UIStoryboard ...
    navigationController?.isNavigationBarHidden = true;
    navigationController?.pushViewController(s, animated: true)
}

It's that easy.

Upvotes: 2

Cy-4AH
Cy-4AH

Reputation: 4586

Don't push view controller. Create new hierarchy with:

Objective-C

[self.navigationController setViewControllers:@[controller] animated:YES];

Swift

navigationController.setViewControllers([controller], animated:true)

Upvotes: 32

FedeH
FedeH

Reputation: 1363

This will hide the back button for the first view.

ClassA* controller = [storyboard instantiateViewControllerWithIdentifier:@"ClassAIdentifier"];

if (controller != nil) {
        [self.navigationController pushViewController:controller animated:YES];
        [controller.navigationItem setHidesBackButton:YES animated:NO];    
    }

You MUST hide the navigationItem after push, otherwise it will be nil.

Upvotes: 0

Rahul Katariya
Rahul Katariya

Reputation: 3628

You need to hide the Navigation Bar in the ViewController before you push the SWRevealViewController by using the below line in viewDidLoad

Objective C

self.navigationController.navigationBarHidden = true;

Swift

self.navigationController?.navigationBarHidden = true;

It will not show the back button in the next view controller pushed

Upvotes: 1

Paulo
Paulo

Reputation: 1245

Use a modal view controller for the login screen so its not part of your navigation controller hierarchy within your navigation root probably in view did load - sample code below:

  - (void) viewDidLoad {
......
  LoginVC *loginViewController = [LoginVC alloc] init];
  loginViewController.parent = self;  
  [self presentViewController: loginViewController animated:YES completion:NULL];
  return;
      }

You may or may not dismiss the modal view from the root - if you do, you will need to be able to call the dismiss routine from loginViewController but there are other ways as well and you can put the dismiss inside the modal view (loginViewController)

  • (void) login_exit:(BOOL)success { [self dismissViewControllerAnimated:YES completion:NULL];

    if !(success) {
    .... send message (UIAlertview and ask if he or she wants to try again) } else { } return;}

Upvotes: 4

Mohit
Mohit

Reputation: 3416

Write this in viewDidLoad method

self.navigationItem.hidesBackButton = YES;

Upvotes: 0

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Very simple.. Just navigate to rootviewcontroller

 SWRevealViewController *swRevealController = (SWRevealViewController *)navVC;
 swRevealController.managedObjectContext = self.managedObjectContext;
 //-- I think above lines not needed as per your question
 [self.navigationController popToRootViewControllerAnimated:YES];

Upvotes: 1

user2071152
user2071152

Reputation: 1195

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

Upvotes: 20

Related Questions