Add method to UINavigationBar back button?

前端 未结 2 1146
无人共我
无人共我 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:24

    Yes you can In viedDidLoad

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

    write following function to check condition

    -(void)goBackToAllPets:(id)sender
    {
        if(/*check condition*/)
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"message" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
            alert.tag = 0;
            [alert show];
    
    
        }
        else
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
    
    
    }
    

提交回复
热议问题