33

I am new to iPhone application development. I want to design an alert view with 2 buttons: OK and Cancel. When the user touches the OK button, then I will print a message that says hello. When they touch the Cancel button, I will print cancel.

Please help; how do I do this?

Ky -
  • 30,724
  • 51
  • 192
  • 308
shreedevi
  • 529
  • 3
  • 9
  • 14
  • Does this answer your question? [How to create an alert box in iphone?](https://stackoverflow.com/questions/5863481/how-to-create-an-alert-box-in-iphone) – Jon Schneider Sep 27 '20 at 21:02

7 Answers7

65

To show the alert:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to say hello?"
                                                message:@"More info..."
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Say Hello",nil];
[alert show];
[alert release];

To respond to whatever button was tapped:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"Cancel Tapped.");
    }
    else if (buttonIndex == 1) {
        NSLog(@"OK Tapped. Hello World!");
    }
}

For more information, see the UIAlertView Class Reference and the UIAlertView Delegate Protocol Reference.

Ky -
  • 30,724
  • 51
  • 192
  • 308
Steve Harrison
  • 121,227
  • 16
  • 87
  • 72
  • 2
    shouldn't your answer also include the necessity for in the .h file? I realize that you reference both the class and delegate protocol, but it's easy to imagine the OP skipping that part :( – KevinDTimm Nov 17 '09 at 09:38
  • it is better to use "buttonIndex!=alertView.cancelButtonIndex", in case you remove the cancel button later and forget to change the button indexes in the delegate call. – Felix Lamouroux Nov 17 '09 at 11:04
  • Steve, you haven't included 'nil' as the final item in your list of otherButtonTitles! Therefore when this code is copied and used, it gives a warning on compile and then crashes on running... – h4xxr Jul 01 '10 at 13:10
  • Now, `[alert release]` won't compile. :) – Ky - Mar 17 '15 at 20:45
42

since the chosen answer is deprecated, here is the new solution:

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                               message:@"This is an alert."
                               preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
   handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

As shown in iOs Developer guide.

krakover
  • 2,989
  • 2
  • 27
  • 29
5

Show the alert with the following snippet

UIAlertView *alert = [[UIAlertView alloc]
   initWithTitle:@"Make an informed choice"
   message:nil
   delegate:self
   cancelButtonTitle:@"Cancel"
   otherButtonTitles:@"OK", nil];
[alert show];

The delegate is set to self so when the alert is dismissed our own class will get a call back. The delegate must implement the UIAlertViewDelegate protocol.

- (void)alertView:(UIAlertView *)alertView
   clickedButtonAtIndex:(NSInteger) buttonIndex{

   if (buttonIndex == 1) {
      // Do it!
   } else {
      // Cancel
   }
}
Niels Castle
  • 8,039
  • 35
  • 56
4

enter image description here

For Objective C:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My Alert"
            message:@"This is an action sheet." 
            preferredStyle:UIAlertControllerStyleAlert]; // 1
    UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"one"
            style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                NSLog(@"You pressed button one");
            }]; // 2
    UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"two"
            style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                NSLog(@"You pressed button two");
            }]; // 3

    [alert addAction:firstAction]; // 4
    [alert addAction:secondAction]; // 5

    [self presentViewController:alert animated:YES completion:nil]; // 6

For Swift:

let alert = UIAlertController(title: "My Alert", message: "This is an action sheet.", preferredStyle: .Alert) // 1
    let firstAction = UIAlertAction(title: "one", style: .Default) { (alert: UIAlertAction!) -> Void in
        NSLog("You pressed button one")
    } // 2

    let secondAction = UIAlertAction(title: "two", style: .Default) { (alert: UIAlertAction!) -> Void in
        NSLog("You pressed button two")
    } // 3

    alert.addAction(firstAction) // 4
    alert.addAction(secondAction) // 5
    presentViewController(alert, animated: true, completion:nil) // 6

PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52
2

Here are a few ways of showing Alert messages on the iPhone

please check this link for more samples and screenshots .

(XCode project with source code included)

  • Simple Action Sheet
  • OK/Cancel Action Sheet
  • Simple Alert

// open a alert with an OK and cancel button

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
        message:@"My message" delegate:self cancelButtonTitle:@"Cancel"
        otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
Adrian Pirvulescu
  • 4,308
  • 3
  • 30
  • 47
0

For debug output you could use (sometimes it happens that you can't use NSLog due to bugs that only appear when app is launched on the device and not from Xcode):

#define MY_ALERT(str) [[[UIAlertView alloc] initWithTitle:@"System Alert" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]

Then, in your code you could do, for example:

MY_ALERT(NSStringFromCGRect(someView.frame))
Lukas Kalinski
  • 2,213
  • 24
  • 26
-1
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Hello world" message:@"This is an alert view" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];

In this way we create an object of class UIAlertView and set the title "Hello world" and the message "This is an alert view " and the title of button as ok. For a detail answer visit this blog

BBog
  • 3,630
  • 5
  • 33
  • 64
yasir
  • 1
  • please add the way for displaying the alert (else your answer is useless) and i will give you a +1 :) – BQuadra Apr 16 '14 at 15:34