如何给导航栏上的backButtonItem设置action?

2024-02-02

如何给导航栏上的backButtonItem设置action?我有一个导航栏,当我按下后退按钮时,我需要向用户提醒一些消息,并且只有在用户做出反应后才返回到上一个视图。我该怎么做?谢谢!

- (void)viewDidLoad 
{
    [super viewDidLoad];

    //no one field don't changed yet
    isDirty = FALSE;

    //edited user
    //set default values
    newData = [data copy];

    //setting navigation controller rigth button
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Save"
                                                                style:UIBarButtonSystemItemDone 
                                                                   target: self 
                                                                   action: @selector(saveBtnUserClick)];
    self.navigationItem.rightBarButtonItem = rightButton; 
    [rightButton release];


    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                   style:UIBarButtonSystemItemDone 
                                                                  target: self 
                                                                  action: @selector(backBtnUserClick)];

    self.navigationItem.backBarButtonItem = leftButton;
    [leftButton release];
}

//以及我的反应方法

-(IBAction) backBtnUserClick
{
    NSLog(@"\n Back pressed");

    //back to previous view
    [self.navigationController popViewControllerAnimated: TRUE];
}

在头文件中添加 并在 .m 中使用它

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem     *)item
{
  //insert your back button handling logic here
  // let the pop happen
  return YES;
}     
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何给导航栏上的backButtonItem设置action? 的相关文章

随机推荐