代码之家  ›  专栏  ›  技术社区  ›  yozhik

如何在导航栏上设置backbuttonem的操作?

  •  2
  • yozhik  · 技术社区  · 15 年前

    如何在导航栏上设置backbuttonem的操作?我有一个导航栏,当我按下后退按钮时,我需要提醒用户一些信息,只有在用户做出反应后-返回到上一个视图。我该怎么做?谢谢!

    - (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];
    }
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Sajjad Ali Tom Harrington    11 年前

    这听起来像是一份工作 UIAlertView . 而不是调用popViewControllerImated: IBAction 方法,alloc/init a UIAlertView 把它呈现出来。然后,当用户点击 UIAlertView ,取消 UIAlertView 打电话来 popViewControllerAnimated: .

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

    在你的 UIAlertViewDelegate 方法调用 PopViewControllerImated: .

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

    要设置“后退”按钮上的操作:

    [[[self navigationController] leftBarButtonItem] setTarget:self];
    [[[self navigationController] leftBarButtonItem] setAction:@selector(backBtnUserClicked:)];
    
        2
  •  11
  •   Christian Loncle    14 年前

    在头文件中添加<UINavigationControllerDelegate>,并在.m中使用它

    - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem     *)item
    {
      //insert your back button handling logic here
      // let the pop happen
      return YES;
    }