Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert view in iphone

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?

like image 243
shreedevi Avatar asked Nov 17 '09 09:11

shreedevi


People also ask

What is a banner alert on iPhone?

Banner alerts are the notifications that appear at the top of your screen while you're using another app on your iPhone or iPad. You see them most often with instant messaging apps, but they can be used for any kind of notification.

How do I show alerts on Lock Screen?

Open your phone's Settings app. Notifications. Under "Lock screen," tap Notifications on lock screen or On lock screen. Choose Show alerting and silent notifications.


3 Answers

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.

like image 160
Steve Harrison Avatar answered Oct 12 '22 06:10

Steve Harrison


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.

like image 40
krakover Avatar answered Oct 12 '22 06:10

krakover


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
   }
}
like image 5
Niels Castle Avatar answered Oct 12 '22 07:10

Niels Castle