您好,登錄后才能下訂單哦!
IOS學習筆記(九)之UIAlertView(警告視圖)和UIActionSheet(操作表視圖)基本概念和使用方法
Author:hmjiangqq
Email:jiangqqlmj@163.com
在平時操作中,應用需要和用戶進行交流:那么會用到下面兩種視圖控件
(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 ofUIActionSheet
).
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的常用屬性和方法:
實例代碼如下:
-(void)showAlertView{ UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil]; [alertView show]; [alertView release]; NSLog(@"showAlertView..."); }
運行截圖:
同時我們可以在創建的時候設置代理(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
看下官方解說:
Use the 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常用屬性和方法
示例代碼如下:
-(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, 透明
};
運行截圖:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。