天天看点

iOS:提示框(警告框)控件UIActionSheet的详解

提示框(警告框)控件2:UIActionSheet

功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能。它与导航栏类似,它继承自UIView。

风格类型:

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. //实例化

  2. //设置风格

 3. //设置代理

  4. //在视图上显示

实现代理方法:

#pragma mark-<UIActionSheetDelegate>

1.点击按钮时触发事件

iOS:提示框(警告框)控件UIActionSheet的详解
iOS:提示框(警告框)控件UIActionSheet的详解

2.取消提示框时触发的事件

3.提示框将要显示时触发的事件

4.显示提示框时触发的方法

5.提示框将要消失时触发的方法

iOS:提示框(警告框)控件UIActionSheet的详解
iOS:提示框(警告框)控件UIActionSheet的详解

6.提示框消失时触发的方法

iOS:提示框(警告框)控件UIActionSheet的详解
iOS:提示框(警告框)控件UIActionSheet的详解

演示结果如下:

iOS:提示框(警告框)控件UIActionSheet的详解

代理方法调用情况如下:

程序运行起来, 没有点击按钮时:

点击任意按钮时:

iOS:提示框(警告框)控件UIActionSheet的详解
iOS:提示框(警告框)控件UIActionSheet的详解

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!

本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4850127.html,如需转载请自行联系原作者

继续阅读