Add method to UINavigationBar back button?

前端 未结 2 1147
无人共我
无人共我 2021-01-25 15:31

How can i add method to UINavigationbar back button, so whenever I click that back button I need to check some values and show UIAlertView? Is there any option for this?

<
2条回答
  •  后悔当初
    2021-01-25 16:28

    Suppose you have two controllers - Controller1 and Controller2. Controller2 is pushed from Controller1. So before pushing the Controller2 on the navigationController from Controller1

    Controller2 *controller2 = [[[Controller2 alloc]  init]autorelease];
    self.navigationItem.hidesBackButton = YES;   
    

    Now, in the viewDidLoad: method of Controller2, add the following snippet

    UIBarButtonItem *backBarButtonItem =[[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBackToAllPets:)]autorelease];
    self.navigationItem.leftBarButtonItem = backBarButtonItem;
    

    and in the backButtonClicked method, you can perform the checks you want to.

提交回复
热议问题