Building Simple Alerts

To create alerts, allocate a UIAlertView object. Initialize it with a title and an array of button titles. The title is an NSString, as are the button titles. In the button array, each string represents a single button that should be shown.

The method snippet shown here creates and displays the simplest alert scenario. It shows a message with a single OK button. The alert doesn’t bother with delegates or callbacks, so its lifetime ends when the user taps a button:

- (void)showAlert:(NSString *)theMessage {     UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Title"         message:theMessage         delegate:nil         cancelButtonTitle:@"OK"         otherButtonTitles:nil];     [av show]; ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.