UIActionSheets
and UIAlertViews
are two slightly obscure but very handy classes that can give your iPhone app a more polished appearance with very little effort. They’re worth the few minutes it takes to read up on them.
UIActionSheets
Action Sheets present a list of options to the user without elaboration. They’re a good way to prompt for confirmation of destructive actions, or to select a subtype of some action. You might create an Action Sheet with code like the following (self
is assumed to be a View Controller that adopts the UIActionSheetDelegate
protocol):
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
The invoking View Controller can handle presses of the “Delete” by defining a method like this:
- (void)actionSheet:(UIActionSheet*)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (!buttonIndex)
{
// A zero button index means the first (i.e. the "Delete") button was pressed,
// so add some application-specific logic here.
}
}
UIAlertViews
Alerts are good ways to inform the user of events that occur without his input. They can also offer several options from which he can choose; they’re sort an an exceptional version of Action Sheets. You can create them with code like the following
UIAlertView* av = [[UIAlertView alloc] initWithTitle:nil
message:@"Something totally happened here, guys"
delegate:nil
cancelButtonTitle:@"Call T-Rex"
otherButtonTitles:nil];
[av show];
[av release];