iOS alertview action on a button

alertview, ios, xcode

Solution

In your code set the UIAlertView delegate:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exit game" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil]; [alert show];

As you have set delegate to self, write the delegate function in the same class as shown below:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) { // Set buttonIndex == 0 to handel "Ok"/"Yes" button response
    // Cancel button response
    }}

Problem

I have a button in a menu which when touched, pops up a alert message with two buttons: "`Cancel`" and "`Yes`". This is the code I have for the alert: ``` UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exit game" message:@"Are you sure?" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil]; [alert show]; ``` Is it possible to add an action to the button "`Yes`"?

Original source