下面提供了使用UIActionSheet的样例代码:
定义UIActionSheet对象
在类中申明UIActionSheet对象,如果需要处理选择UIActionSheet项后的消息,则需要使用UIActionSheetDelegate。
@interface MyClassController : UIViewController <UIActionSheetDelegate>
{
UIActionSheet *sheet;
}
@end
创建和显示UIActionSheet
在创建UIActionSheet对象时,指定接收UIActionSheetDelegate代理对象。
sheet = [[UIActionSheet alloc] initWithTitle:@"Select Belgian Beer Style"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Dubble", @"Lambic", @"Quadrupel", @"Strong Dark Ale", @"Tripel", nil];
// Show the sheet
[sheet showInView:self.view];
[sheet release];
判断哪一个选项被选中,在MyClassController中实现下面方法,在此方法中实现需要处理的逻辑。
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"Button %d", buttonIndex);
}