Camerz007
Camerz007

Reputation: 61

How to have a UIBarButton execute a custom function?

I'm hoping someone can help me figure this out...I'm a beginner Xcode / Objective-C programmer. I'm working on a app that is a continuation of last semester.

1: So I created a button and I need it to execute this custom function:

- (void)cancelTapped {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"];
    [alert setTag:1];
    [alert show];
}

How/where do I put this function? Is it in the button properties? Or would I write this in a custom class/controller and link it to it?

2: How do I get it to listen for the alert to return on: - alertView:didDismissWithButtonIndex:

3: From there, how would I would write the logic to hide the page and pop the view?

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1 && buttonIndex == 1) {
        // Delete data and return to lobby
        [self.navigationController popViewControllerAnimated:YES];
    }
}

Upvotes: 0

Views: 130

Answers (2)

Ken Toh
Ken Toh

Reputation: 3751

You will need a custom UIViewController to house the logic for your button and alert view interactions. I am assuming you know how to do that.

Once done, assuming you have a reference to your button property in your view controller, you can programatically add a target to your button and pass the selector cancelTapped as a parameter:

[myButton addTarget:self action:@selector(cancelTapped) forControlEvents:UIControlEventTouchUpInside];

Alternatively you could control-drag from the button in your Storyboard to the header file of your custom UIViewController, and define an IBAction. That will create an empty cancelTapped method in your implementation which you could then add your logic in.

As for listening on UIAlertView messages, you will need to make your custom UIViewController a delegate of the UIAlertView by passing "self" as the delegate in the following statement:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"My Message" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"];

Your CustomViewController should also be declared as a UIAlertViewDelegate.

CustomViewController.h

@interface CustomViewController : UIViewController<UIAlertViewDelegate>
@end

Hope this helps!

Upvotes: 1

Kumar KL
Kumar KL

Reputation: 15335

By the using VIewWillDisappear method to detect the press of The back button of NavigationItem:

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
       // Navigation button was pressed. Do some stuff 
      [self cancelTapped];

    }
    [super viewWillDisappear:animated];
}
- (void)cancelTapped {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"];
    [alert setTag:1];
    [alert show];
}

For More info && also Custom UIBArButtonItem Check Here

Upvotes: 0

Related Questions