Alex Rivera | Logout

How to write event handlers for buttons in UIAlertView?

Asked 2011-09-13T05:11:05.037
21

Say I have a alert view like follows in obj c

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"download"];
        [alert show];
        [alert release];

Now we have 2 buttons on the alert view (Ok & Download), how to write an event handler for the Download one?

Edit
Report

1 Answer

46

First you will need to add the UIAlertViewDelegate to your header file like below:

Header file (.h)

@interface YourViewController : UIViewController<UIAlertViewDelegate> 

Implementation File (.m)

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"download"];
        [alert show];
        [alert release];

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        //Code for OK button
    }
    if (buttonIndex == 1)
    {
        //Code for download button
    }
}
answered 2011-09-13T05:16:36.790

Your Answer