yozhik
yozhik

Reputation: 5064

How to set action to the backButtonItem on the navigation bar?

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

//and my method for reaction

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

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

Upvotes: 2

Views: 13273

Answers (3)

Ely
Ely

Reputation: 9121

When using iOS 16 or newer, you can insert custom logic by setting the backAction of the navigation bar. Example:

navigationItem.backAction = UIAction(handler: { [weak self] action in
    
    // Your custom action here
    self?.myAlertAction()

    // Navigate one view controller back
    self?.navigationController?.popViewController(animated: true)
})

Upvotes: 0

Rob Jones
Rob Jones

Reputation: 4985

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

In your UIAlertViewDelegate methods call 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:)];

Upvotes: 3

Christian Loncle
Christian Loncle

Reputation: 1584

Add the < UINavigationControllerDelegate > in the header file and use this in the .m

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

Upvotes: 11

Related Questions