UIActionSheet Example > Use more ActionSheets!

UIActionSheet provides a great way to supply the iPhone user with a series of options. You can't find them in Interface Builder and they haven't made it into any of the example apps in the iPhoneDev center. To add to their usefulness you may choose the type and number of buttons shown.
Here is a quick rundown of how to create a UIActionSheet on an instance of UIViewController:
1) In your viewController.h file add the property
UIActionSheet* actionSheet;
2) Instantiate your UIActionSheet in the loadView method
- (void)loadView {
[super loadView];
actionSheet = [[UIActionSheet alloc] initWithTitle:@"This is an ActionSheet" delegate:self cancelButtonTitle:@"Option One" destructiveButtonTitle:@"Option Two" otherButtonTitles:nil]; }
3) Display your action sheet in viewDidLoad
// Implement viewDidLoad if you need to do additional setup after loading the view.
- (void)viewDidLoad {
[super viewDidLoad];
//Wrong! [self.view addSubview:actionSheet];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
//
}
That's it. Now you can use them everywhere.
Note: If you choose to not use the default cancel/destruct buttons create your ActionSheet as follows:
actionSheet = [[UIActionSheet alloc] initWithTitle:@"What would you like to do today?"
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil
otherButtonTitles:@"DVD's", @"MKV's", @"Blu Ray's", @"all",nil];
Labels: iPhone

