IOS UIAlertView 提示視圖

來源:互聯網
上載者:User

標籤:

一 UIAlertView 簡介    

如果需要彈出訊息讓使用者確認,或者要求使用者輸入帳戶密碼,其他本文,則可用用UIAlertView。


二 UIAlertView 建立
    /**     1.建立 UIAlertView          title                  提示視表徵圖題,比如 警示、提示、異常     message                使用者看的實際訊息     delegate               選擇性參數,傳遞委派物件給提示視圖,當檢視狀態變更時,委派物件會被通知。傳遞的參數對象必須實現 UIAlertViewDelegate 協定     cancelButtonTitle      選擇性參數,這個字串符會顯示在提示示視圖的取消按鈕上。          otherButtonTitles      選擇性參數,若你希望提示示視圖出現其他按鈕,只要傳遞標題參數,此參數需用逗號分隔,用 nil 做結尾。          */    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"                                                        message:@"Message"                                                       delegate:nil                                               cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

  

    

三 設定樣式
  /**     2. 設定樣式     UIAlertViewStyleDefault = 0,            預設,沒有輸入框     UIAlertViewStyleSecureTextInput,        提示視圖中添加密碼框     UIAlertViewStylePlainTextInput,         提示視圖中添加輸入框     UIAlertViewStyleLoginAndPasswordInput   登入和密碼框          */    [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

    

    

四 展示 
    [alertView show];

    



五 監聽點擊,並擷取使用者的輸入

如果要監聽使用者的點擊和擷取使用者輸入,需要實現UIAlertViewDelegate 協議,協議中的alertView:clickedButtonAtIndex 方法可以得到使用者在提示視圖上所按的按鈕,按鈕的索引值會被儲存在變數 clickedAtIndex 中

/** *  監聽點擊 * *  @param alertView   <#alertView description#> *  @param buttonIndex <#buttonIndex description#> */-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];        // 判斷點擊    if ([buttonTitle isEqualToString:@"Cancel"]){        NSLog(@"User pressed the Cancel button.");    }    else if ([buttonTitle isEqualToString:@"Ok"]){        NSLog(@"User pressed the Ok button.");    }            //接受輸入類容    //textFieldAtIndex 擷取對應位置的UITextField    UITextField *textField = [alertView textFieldAtIndex:0];    NSLog(@"%@",textField.text);        UITextField *textField2 = [alertView textFieldAtIndex:1];    NSLog(@"%@",textField2.text);}




六 完整代碼
#import "ViewController.h"@interface ViewController ()<UIAlertViewDelegate>@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];            //UIAlertView 作用    //如果需要彈出訊息讓使用者確認,或者要求使用者輸入帳戶密碼,其他本文,則可用用UIAlertView        /**     1.建立 UIAlertView          title                  提示視表徵圖題,比如 警示、提示、異常     message                使用者看的實際訊息     delegate               選擇性參數,傳遞委派物件給提示視圖,當檢視狀態變更時,委派物件會被通知。傳遞的參數對象必須實現 UIAlertViewDelegate 協定     cancelButtonTitle      選擇性參數,這個字串符會顯示在提示示視圖的取消按鈕上。          otherButtonTitles      選擇性參數,若你希望提示示視圖出現其他按鈕,只要傳遞標題參數,此參數需用逗號分隔,用 nil 做結尾。          */    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"                                                        message:@"Message"                                                       delegate:nil                                               cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];                /**     2. 設定樣式     UIAlertViewStyleDefault = 0,            預設,沒有輸入框     UIAlertViewStyleSecureTextInput,        提示視圖中添加密碼框     UIAlertViewStylePlainTextInput,         提示視圖中添加輸入框     UIAlertViewStyleLoginAndPasswordInput   登入和密碼框          */    [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];                        /**     3. 監聽點擊          如果要監聽使用者的點擊需要實現UIAlertViewDelegate 協議,協議中的alertView:clickedButtonAtIndex 方法可以得到使用者在提示視圖上所按的按鈕,按鈕的索引值會被儲存在變數 clickedAtIndex 中     */    [alertView setDelegate:self];//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"//                                                        message:@"Message"//                                                       delegate:self//                                              cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];        /**     4. 接受AlertView 輸入類容     */                //展示    [alertView show];    }/** *  監聽點擊 * *  @param alertView   <#alertView description#> *  @param buttonIndex <#buttonIndex description#> */-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];        // 判斷點擊    if ([buttonTitle isEqualToString:@"Cancel"]){        NSLog(@"User pressed the Cancel button.");    }    else if ([buttonTitle isEqualToString:@"Ok"]){        NSLog(@"User pressed the Ok button.");    }            //接受輸入類容    UITextField *textField = [alertView textFieldAtIndex:0];    NSLog(@"%@",textField.text);        UITextField *textField2 = [alertView textFieldAtIndex:1];    NSLog(@"%@",textField2.text);}@end


IOS UIAlertView 提示視圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.