UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。
要遵循UIActionSheetDelegate协议;
需#import<UIKit/UIKit.h>
需注意:
Important: UIActionSheet
is deprecated in iOS 8. (Note that UIActionSheetDelegate
is also deprecated.) To create and manage action sheets in iOS 8 and later, instead use UIAlertController
with a preferredStyle
of UIAlertControllerStyleActionSheet
.
如上所示,在IOS8.0及以后 版本中不再支持UIActionSheet,以后的研究下用UIAlertController实现的方法。
重要的属性及方法:
- - (instancetype)initWithTitle:(NSString *)title//标题
delegate:(id<UIActionSheetDelegate>)delegate//委托对象,需满足UIActionSheetDelegate协议,可设为nil
cancelButtonTitle:(NSString *)cancelButtonTitle//取消标题
destructiveButtonTitle:(NSString *)destructiveButtonTitle//‘确认’标题
otherButtonTitles:(NSString *)otherButtonTitles//其他按钮标题,可以是一组
, … //构造方法
- @property(nonatomic, copy) NSString *title //标题;
- @property(nonatomic) UIActionSheetStyle actionSheetStyle //显示风格,可选项如下
- UIActionSheetStyleAutomatic=-1
- UIActionSheetStyleDefault=UIBarStyleDefault
- UIActionSheetStyleBlackTranslucent=UIBarStyleBlackTranslucent
- UIActionSheetStyleBlackOpaque=UIBarStyleBlackOpaque
- - (NSInteger)addButtonWithTitle:(NSString *)title //增加一个按钮
- @property(nonatomic, readonly) NSInteger numberOfButtons//按钮总数,意义不大
- @property(nonatomic) NSInteger cancelButtonIndex//取消按钮的序号,可用来确定当按下是不是取消按钮
- @property(nonatomic) NSInteger destructiveButtonIndex//‘确认’按钮序号,可用来确定当按下是不是‘确认’按钮
- - (void)showInView:(UIView *)view // view The view from which the action sheet originates.
常用委托方法:
- - (void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
Parameters
actionSheet | The action sheet that was dismissed. |
buttonIndex | The index of the button that was clicked. The button indices start at 0. If this is the cancel button index, the action sheet is canceling. If -1, the cancel button index is not set. |