Sunday, July 25, 2010

How to show alert for iOS?

This is how you can show an alert box in iPhone or iPad app.


In your code include the following:

UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"Confirm"];
[alert setMessage:@"Buy This?"];
[alert setDelegate:self];
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
[alert show];
[alert release];

Then implement this in the delegate, in this case, the controller which calls UIAlertView.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
// Yes, do something
NSLog(@"Yes!");
}
else if (buttonIndex == 1)
{
// No
NSLog(@"No!");
}
}

No comments:

Post a Comment