天天看點

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,如需轉載請自行聯系原作者

繼續閱讀