1

I've created a UIAlertView and now I want to check which button the user presses.

My code is:

- (IBAction)button1 {
    {
        UIAlertView *alert1 = [[UIAlertView alloc] init];
        [alert1 setTitle:@"Hello"];
        [alert1 setMessage:@"Do you like smoking?"];
        [alert1 addButtonWithTitle:@"Yes"];
        [alert1 addButtonWithTitle:@"No"];
        [alert1 show];
    }
}

How can I check it with if-else statement?

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
useboot
  • 67
  • 1
  • 5

4 Answers4

4

You must set the delegate of your UIAlertView to the class that will handle the callbacks from the UIAlertView itself

E.g.

[alert1 setDelegate:self];

Where self is your current UIViewController that implements the protocol <UIAlertViewDelegate>.

When the user taps a button, the UIAlertView will call back to whatever object you set as the delegate, in my example we are using the UIViewController that created the UIAlertView. Once we get the callback, we can check which button index has been tapped and act accordingly. This is a basic delegation pattern that is used throughout iOS development, especially UIKit.

Example

@interface MyViewController : UIViewController() <UIAlertViewDelegate>
@end

@implementation MyViewController

- (IBAction)button1 {
    {
        UIAlertView *alert1 = [[UIAlertView alloc] init];
        [alert1 setTitle:@"Hello"];
        [alert1 setMessage:@"Do you like smoking?"];
        [alert1 addButtonWithTitle:@"Yes"];
        [alert1 addButtonWithTitle:@"No"];
        [alert1 setDelegate:self];
        [alert1 show];
    }
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   // Handle interaction
       switch (buttonIndex)
       {
           case 0:
             NSLog(@"Yes was pressed");
             break;
           case 1:
             NSLog(@"No was pressed");
             break;
       }
}

@end

It is very important you specify in the header file, or class interface extension that your class implements the <UIAlertViewDelegate>.

I recommend you see the UIAlertViewDelegate Protocol Reference for more information, and you can follow this approach for many other UIKit components.

Tim
  • 8,932
  • 4
  • 43
  • 64
  • I know this is a year old, but even some of the code I find that is that old doesn't work. iOS 8.1 & Xcode 6.1 tested & works! :) Although I personally had to add in two more if's, still applied it, and runs amazingly! Thanks!!! – Erik Bean Oct 28 '14 at 23:06
  • Glad to hear it. Although you should probably consider moving to `UIAlertController` now. – Tim Oct 29 '14 at 09:33
  • I saw that yesterday while rummaging through the Apple Docs. Will this still work with it? – Erik Bean Oct 29 '14 at 13:59
  • UIAlertController works slightly differently, as it is an entire UIViewController that you present modally. Definitely worth reading up on, but it is straight forward to transition. – Tim Oct 29 '14 at 17:24
2

Implement UIAlertViewDelegate protocol in your view controller, set it as a delegate to the UIAlertView, and wait for the alertView:clickedButtonAtIndex: event, like this:

@interface MyViewController : UIViewController <UIAlertViewDelegate>
-(void)alertView:(UIAlertView *)alertView
      clickedButtonAtIndex:(NSInteger)buttonIndex;
@end

@implementation MyViewController

-(void)alertView:(UIAlertView *)alertView
    clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"Button %d clicked...", (int)buttonIndex);
}

@end

Change the code that displays the alert view as follows:

UIAlertView *alert1 = [[UIAlertView alloc] init];
[alert1 setTitle:@"Hello"];
[alert1 setMessage:@"Do you like smoking?"];
[alert1 addButtonWithTitle:@"Yes"];
[alert1 addButtonWithTitle:@"No"];
alert1.delegate = self; // <<== Add this line
[alert1 show];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Adding on to the answer by Jeff, I think that you have to enable the other button in order to place your logic when the button is clicked.

In order to creating the button with your code:

- (IBAction)button1 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
                                                        message:@"Do you like smoking?"
                                                       delegate:self
                                              cancelButtonTitle:@"Yes"
                                              otherButtonTitles:@"No", nil];
    [alert show];
}

But before knowing which button is clicked, you have to enable the first other button, by calling this delegate method:

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    return YES;
}

This will enable the NO button that you have created. Then, you will be able to do some logic with the clickedButtonAtIndex method, which I suppose you will be doing.

Implement the UIAlertView delegate method:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        // i love smoking!
    } else if (buttonIndex == 1) {
        // i hate smoking!
    }
}

Be sure to declare the UIAlertViewDelegate in your header class.

Make sure that the alertViewShouldEnableFirstOtherButton: method returns YES, if not you will not be able to put in your logic for when the button is pressed.

Hope this helps! :)

faterpig
  • 344
  • 1
  • 6
0

The best thing you can do is make the class that presents your alert view conform to the UIAlertViewDelegate protocol and implement the method –alertView:clickedButtonAtIndex:. That way you'll know what button was clicked.

Hope this helps!

LuisCien
  • 6,362
  • 4
  • 34
  • 42