標籤:
提示框(警告框)控制項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. //執行個體化
//建立提示框執行個體 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:");}
示範結果如下:
代理方法調用情況如下:
程式運行起來, 沒有點擊按鈕時:
2015-09-30 19:34:29.647 提示框UIActionSheet[3866:212744] willPresentActionSheet:
點擊任意按鈕時:
2015-09-30 19:35:18.882 提示框UIActionSheet[3866:212744] actionSheet: clickedButtonAtIndex:2015-09-30 19:35:18.883 提示框UIActionSheet[3866:212744] actionSheet: willDismissWithButtonIndex:2015-09-30 19:35:19.297 提示框UIActionSheet[3866:212744] actionSheet: didDismissWithButtonIndex:
iOS:提示框(警告框)控制項UIActionSheet的詳解