如何在导航栏上对backButtonItem设置操作? [英] How to set action to the backButtonItem on the navigation bar?

查看:92
本文介绍了如何在导航栏上对backButtonItem设置操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to set action to the backButtonItem on the navigation bar? I have a navigation bar, when I'm pressing the back button, I need to alert some message to the user, and only after user's reaction - return to the previous view. How can I do it? Thanx!

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

推荐答案

这听起来像UIAlertView的工作.与其调用popViewControllerAnimated:在您的IBAction方法中,分配/初始化一个UIAlertView并显示它.然后,当用户点击UIAlertView上的按钮时,关闭UIAlertView并调用popViewControllerAnimated:.

This sounds like a job for UIAlertView. Instead of calling popViewControllerAnimated: in your IBAction methods, alloc/init a UIAlertView and present it. Then, when the user taps a button on the UIAlertView, dismiss the UIAlertView and call popViewControllerAnimated:.

- (IBAction)backBtnUserClicked:(id)object {
    UIAlertView *av = [[[UIAlertView alloc] initWithMessage:@"Wait!"
          delegate:self
               cancelButtonTitle:@"Ok"
               otherButtonTitles:nil] autorelease];
   [av show];
}

在您的UIAlertViewDelegate方法中,调用popViewControllerAnimated:.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [[self navigationController] popViewControllerAnimated:YES];
}

要在后退"按钮上设置操作,请执行以下操作:

To set the action on the back button:

[[[self navigationController] leftBarButtonItem] setTarget:self];
[[[self navigationController] leftBarButtonItem] setAction:@selector(backBtnUserClicked:)];

这篇关于如何在导航栏上对backButtonItem设置操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆