Make UIAlertView Button trigger function On Press
NOTE:
Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.
Please check this out tutorial
Objectvie C
.h file
@interface urViewController : UIViewController <UIAlertViewDelegate> {
.m file
// Create Alert and set the delegate to listen eventsUIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete" message:@"Press OK to submit your data!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];// Set the tag to alert unique among the other alerts.// So that you can find out later, which alert we are handlingalert.tag = 100;[alert show];//[alert release];-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ // Is this my Alert View? if (alertView.tag == 100) { //Yes // You need to compare 'buttonIndex' & 0 to other value(1,2,3) if u have more buttons. // Then u can check which button was pressed. if (buttonIndex == 0) {// 1st Other Button [self submitData]; } else if (buttonIndex == 1) {// 2nd Other Button } } else { //No // Other Alert View }}
Swift
The Swifty way is to use the new UIAlertController and closures:
// Create the alert controller let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) // Create the actions let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { UIAlertAction in NSLog("OK Pressed") } let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { UIAlertAction in NSLog("Cancel Pressed") } // Add the actions alertController.addAction(okAction) alertController.addAction(cancelAction) // Present the controller self.presentViewController(alertController, animated: true, completion: nil)
If you are using multiple UIAlertView instances that are not declared in the class's interface you can also set a tag to identify instances in your delegate method, for example:
somewhere on top of your class file myClass.m
#define myAlertViewsTag 0
creating the UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert" message:@"please press ok or cancel" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];alert.tag = myAlertViewsTag;[alert show];[alert release];
the delegate method:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (alertView.tag == myAlertViewsTag) { if (buttonIndex == 0) { // Do something when cancel pressed } else { // Do something for ok } } else { // Do something with responses from other alertViews }}
You need to set the delegate when allocating the alertview, then use one of the UIAlertViewDelegate methods to call your own method, for example:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete" message:@"Press OK to submit your data!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];[alert show];[alert release];- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ [self submitData];}