提示框(警告框)控件2:UIActionSheet
typedef NS_ENUM(NSInteger, UIActionSheetStyle) {
UIActionSheetStyleAutomatic = -1, //iOS系统自动默认的风格
UIActionSheetStyleDefault = UIBarStyleDefault,// 默认风格,灰色背景上显示白色文字
UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent //透明黑色背景,白色文字
UIActionSheetStyleBlackOpaque = UIBarStyleBlackOpaque, //纯黑背景,白色文字
};
属性:
@property(nonatomic,assign) id<UIActionSheetDelegate> delegate; // 代理
@property(nonatomic,copy) NSString *title; //标题
@property(nonatomic) UIActionSheetStyle actionSheetStyle; //风格类型
@property(nonatomic,readonly) NSInteger numberOfButtons; //按钮数量
@property(nonatomic) NSInteger cancelButtonIndex; //取消按钮的索引
@property(nonatomic) NSInteger destructiveButtonIndex; //红色按钮索引
@property(nonatomic,readonly) NSInteger firstOtherButtonIndex; //其他第一个按钮索引
@property(nonatomic,readonly,getter=isVisible) BOOL visible; //是否可见
方法:
※创建实例的初始化方法
- (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...("Use UIAlertController instead.”);
※添加按钮,返回它的索引
- (NSInteger)addButtonWithTitle:(NSString *)title;
※返回指定索引的按钮文字
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
※显示在工具栏
- (void)showFromToolbar:(UIToolbar *)view;
※显示在导航栏
- (void)showFromTabBar:(UITabBar *)view;
※显示在工具栏按钮上
- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated ;
※在指定区域和视图上显示
- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated;
※在视图上显示
- (void)showInView:(UIView *)view;
※按钮消失
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
代理方法:
@protocol UIActionSheetDelegate <NSObject>
@optional
//点击一个按钮时触发的方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
// 如果没有定义的委托,我们模拟一个点击取消按钮
- (void)actionSheetCancel:(UIActionSheet *)actionSheet;
//将要显示警告框时触发的方
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;
//已经显示提示框时触发的方法
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet;
//提示框将要消失时触发的方法
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;
//提示框已经消失时触发的方法
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;
@end
举例如下:类实现协议
@interface ViewController ()<UIActionSheetDelegate>
1. //实例化
//创建提示框实例
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"title" delegate:self cancelButtonTitle:@"cance" destructiveButtonTitle:@"destructive" otherButtonTitles:@"button1", @"button2",nil];
2. //设置风格
//设置风格
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
3. //设置代理
//设置代理
actionSheet.delegate = self;
4. //在视图上显示
//在视图上显示
[actionSheet showInView:self.view];
实现代理方法:
#pragma mark-<UIActionSheetDelegate>
1.点击按钮时触发事件
//点击一个按钮时触发的方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"actionSheet: clickedButtonAtIndex:");
}
2.取消提示框时触发的事件
// 如果没有定义的委托,我们模拟一个点击取消按钮
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
NSLog(@"actionSheetCancel:");
}
3.提示框将要显示时触发的事件
//将要显示警告框时触发的方
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
NSLog(@"willPresentActionSheet:");
}
4.显示提示框时触发的方法
//已经显示提示框时触发的方法
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
{
NSLog(@"didPresentActionSheet:");
}
5.提示框将要消失时触发的方法
//提示框将要消失时触发的方法
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"actionSheet: willDismissWithButtonIndex:");
}
6.提示框消失时触发的方法
//提示框已经消失时触发的方法
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"actionSheet: didDismissWithButtonIndex:");
}
演示结果如下:
代理方法调用情况如下:
程序运行起来, 没有点击按钮时:
-- ::29.647 提示框UIActionSheet[:] willPresentActionSheet:
点击任意按钮时:
-- ::18.882 提示框UIActionSheet[:] actionSheet: clickedButtonAtIndex:
-- ::18.883 提示框UIActionSheet[:] actionSheet: willDismissWithButtonIndex:
-- ::19.297 提示框UIActionSheet[:] actionSheet: didDismissWithButtonIndex: