Alert views are pop-up views that appear over the current view on the iPhone.
Creating and showing an alert (ARC compatible ):
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Really reset? "Message: @" Do you really want to reset this game? "Delegate: self cancelButtonTitle: @" Cancel "otherButtonTitles: nil];
// Optional-add more buttons:
[Alert addButtonWithTitle: @ "Yes"];
[Alert show];
For non-ARC (retain/release) projects, you must autorelease the alert view:
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Really reset? "Message: @" Do you really want to reset this game? "Delegate: self cancelButtonTitle: @" Cancel "otherButtonTitles: nil] autorelles];
// Optional-add more buttons:
[Alert addButtonWithTitle: @ "Yes"];
[Alert show];
If you add the UIAlertViewDelegate protocol to your controller, you can also add the following method which is called after the user dismisses the alert view:
-(Void) alertView :( UIAlertView *) alertView didDismissWithButtonIndex :( NSInteger) buttonIndex {
If (buttonIndex = 1 ){
// Do stuff
}
}
Button indices start at 0 (for the cancelButton specified in the alloc/init), and go up by 1 for each addButtonWithTitle call you add. if you have a lot of alerts, your didDismiss method can keep track of which one is being dismissed if you add the setTag call to the alert initialization: [alert setTag: 23];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Error" message: @ "I'm sorry Dave, I'm afraid I can't do that. "delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil] autorelles];
[Alert setTag: 12];
[Alert show];
... Later...
-(Void) alertView :( UIAlertView *) alertView didDismissWithButtonIndex :( NSInteger) buttonIndex {
If ([alertView tag] = 12) {// it's the Error alert
If (buttonIndex = 0) {// and they clicked OK.
// Do stuff
}
}
}
Additional References