標籤:
一 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 提示視圖