Push to ViewController without back button

2020-05-23 09:26发布

问题:

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.

The code used to switch screens is as follows -

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

回答1:

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


回答2:

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

Objective-C

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

Swift

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


回答3:

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



回答4:

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


回答5:

Write this in viewDidLoad method

self.navigationItem.hidesBackButton = YES;


回答6:

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



回答7:

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.