Override back button in navigation stack while keeping appearance of default back button?

后端 未结 8 2100
野的像风
野的像风 2020-12-08 22:38

How do I override the back button for just one view (not for all the back buttons present in different views) such that on click of the back button, root view controller is

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 23:07

    You need to replace the backbutton and associate an action handler:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // change the back button to cancel and add an event handler
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@”back”
                                                                       style:UIBarButtonItemStyleBordered
                                                                      target:self
                                                                      action:@selector(handleBack:)];
    
        self.navigationItem.leftBarButtonItem = backButton;
    }
    
    - (void)handleBack:(id)sender {
        // pop to root view controller
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    

提交回复
热议问题