UIActionSheet是一个操作表的控件。能够在保证用户体验的情况下提供多个按钮。常见的“分享到”便是UIActionSheet组件。
当设备是Iphone时,UIActionSheet是从屏幕下方向上滑出,而当是Ipad时,则会在触发UIActionSheet的按钮周围出现。且当是Ipad时,不会有“取消”按钮,即使在程序中进行了定义,也不会显示。
UIActionSheet的委托协议是UIActionSheetDelegate。当要监听哪个按钮被点击时,需要实现actionSheet:clickedButtonAtIndex方法。参数为按钮编号,顺序为从上往下。
示例代码:
- UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"facebook", @"腾讯微博", nil nil];
- actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
- [actionSheet showInView:self.view];
其中,actionSheetStyle用以指明操作表所使用的样式。操作表样式分别包含:UIActionSheetStyleAutomatic,UIActionSheetStyleDefault,UIActionSheetStyleBlackTranslucent,UIACtionSheetStyleBlackOpaque四种。
代码中的destructiveButton表示破坏性按钮。破坏性按钮会以红色显示。若没有这类按钮,则设置为nil。
一个操作表中破坏性按钮只能有一个。