天天看點

IOS學習筆記(九)之UIAlertView(警告視圖)和UIActionSheet(操作表視圖)基本概念和使用方法

IOS學習筆記(九)之UIAlertView(警告視圖)和UIActionSheet(操作表視圖)基本概念和使用方法

     Author:hmjiangqq

     Email:[email protected]

在平時操作中,應用需要和使用者進行交流:那麼會用到下面兩種視圖控件

(1:警告視圖UIAlertView,2:操作表視圖UIActionSheet)

(一):警告視圖:UIAlertView

首先看下官方解說:

 Use the

UIAlertView

class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an   instance of

UIActionSheet

).

 Use the properties and methods defined in this class to set the title, message, and delegate of an alert view and configure the buttons. You must set a delegate if you add custom buttons. The delegate should conform to the

UIAlertViewDelegate

protocol.   Use the

show

method   to display an alert view once it is configured.

作用:提示使用者,幫助使用者和選擇,用于警告或者提示。最多是兩個按鈕。

如果超過兩個就應該使用第二種方式:操作表。

[注]在IOS開發中,警告框的權限比較高,是以我們不應該去随意的使用。一般情況下,

警告框使用的場景可以是如下的幾個:

①:應用不能運作:例如:應用崩潰,或者無法擷取資料,這裡可以給使用者一個警告。

②:詢問使用者:  例如:應用不能繼續運作的時候,可以提示讓使用者去選擇解決方案。

③:詢問一些授權資訊  :例如:我們的應用需要去通路以下使用者的隐私資料,這裡提示給

   使用者,友善使用者選擇。

④....等等其他場景。

UIAlertView的常用屬性和方法:

IOS學習筆記(九)之UIAlertView(警告視圖)和UIActionSheet(操作表視圖)基本概念和使用方法
執行個體代碼如下:

-(void)showAlertView{     UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];     [alertView show];     [alertView release];       NSLog(@"showAlertView..."); }      

運作截圖:

IOS學習筆記(九)之UIAlertView(警告視圖)和UIActionSheet(操作表視圖)基本概念和使用方法

同時我們可以在建立的時候設定代理(Delegate)這邊用self指定,為目前控制器實作代理方法:

UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil]; 實作方法如下: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{     NSLog(@"點選了:%@",[alertView buttonTitleAtIndex:buttonIndex]);      }      

(二):操作表視圖:UIActionSheet

看下官方解說:

UIActionSheet

class to present the user with a set of alternatives for how to proceed with a given task. You can also use action sheets to prompt   the user to confirm a potentially dangerous action. The action sheet contains an optional title and one or more buttons, each of which corresponds to an action to take.

 Use the properties and methods of this class to configure the action sheet’s message, style, and buttons before presenting it. You should also assign a delegate to your action sheet. Your delegate object is responsible for performing the action associated with   any buttons when they are tapped and should conform to the

UIActionSheetDelegate

protocol.   For more information about implementing the methods of the delegate, see UIActionSheetDelegate   Protocol Reference.

用于給使用者多個提示選擇操作,例如在我們的應用中,我們需要把資訊分享到微信,騰新微網誌

等多個平台,就應該使用操作表。操作表示是UIActionSheet建立,會從手機的螢幕底下滑出來。

UIActionSheet常用屬性和方法

IOS學習筆記(九)之UIAlertView(警告視圖)和UIActionSheet(操作表視圖)基本概念和使用方法

示例代碼如下:

-(void)showActionSheet{     //操作表     UIActionSheet *actionSheet=[[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"按鈕" otherButtonTitles:@"微信",@"新浪", nil]autorelease];     actionSheet.actionSheetStyle=UIActionSheetStyleBlackOpaque;     [actionSheet showInView:self.window];     NSLog(@"showActionSheet..."); } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{     NSLog(@"點選了:%@",[alertView buttonTitleAtIndex:buttonIndex]);      }      

[注]:UIActionSheet的actionSheetStyle屬性用于設定操作表的樣式,樣式風格如下:

typedefNS_ENUM(NSInteger,   UIActionSheetStyle) {

   UIActionSheetStyleAutomatic        = -1,       // take appearance from toolbar style otherwise   uses 'default'

     UIActionSheetStyleDefault          = UIBarStyleDefault, 預設樣式

     UIActionSheetStyleBlackTranslucent = UIBarStyleBlackTranslucent,  半透明

     UIActionSheetStyleBlackOpaque      = UIBarStyleBlackOpaque,  透明

 };

運作截圖:

IOS學習筆記(九)之UIAlertView(警告視圖)和UIActionSheet(操作表視圖)基本概念和使用方法