UIAlertView筆記


1. 最簡單的用法

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 

                                                  message:@"這是一個簡單的警告框!" 

                                                  delegate:nil   

                                                  cancelButtonTitle:@"確定" 

                                                  otherButtonTitles:nil];  

[alert show];  

 

 

2. 為UIAlertView添加多個按鈕

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 

                                                  message:@"請選擇一個按鈕:" 

                                                  delegate:nil   

                                                  cancelButtonTitle:@"取消" 

                                                  otherButtonTitles:@"按鈕一", 
                                                                          @"按鈕二",
                                                                          @"按鈕三", nil]
[alert show];                      

 

 

 3. 如何判斷用戶點擊的按鈕

UIAlertView有一個委托UIAlertViewDelegate ,繼承該委托來實現點擊事件

頭文件:

@interface MyAlertViewViewController : UIViewController<UIAlertViewDelegate> {

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

-(IBAction) buttonPressed;

@end


源文件:

-(IBAction) buttonPressed

{

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 

                                                 message:@"請選擇一個按鈕:" 

                                                 delegate:self   

                                                 cancelButtonTitle:@"取消" 

                                                 otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil];  

[alert show];  

}

 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d個按鈕!",buttonIndex];

 

UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                 message:msg

                                                 delegate:nil

                                                 cancelButtonTitle:@"確定"

                                                 otherButtonTitles:nil];

 

[alert show];

}

 

點擊“取消”,“按鈕一”,“按鈕二”,“按鈕三”的索引buttonIndex分別是0,1,2,3

 

4.修改提示框樣式

iOS5中UIAlertView新增了一個屬性alertViewStyle,它的類型是UIAlertViewStyle,是一個枚舉值:

typedef enum {
    UIAlertViewStyleDefault = 0,
    UIAlertViewStyleSecureTextInput,
    UIAlertViewStylePlainTextInput,
    UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle;

alertViewStyle屬性默認是UIAlertViewStyleDefault。我們可以把它設置為UIAlertViewStylePlainTextInput,那么AlertView就顯示為這樣:

UIAlertViewStyleSecureTextInput顯示為:

UIAlertViewStyleLoginAndPasswordInput為:

 

當然我們也可以通過創建UITextField來關聯這里的輸入框並設置鍵盤響應的樣式

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"CD-KEY" 

                                                 message:@"please enter cd-key:" 

                                                 delegate:self   

                                                 cancelButtonTitle:@"Cancel" 

                                                 otherButtonTitles:@"Ok",nil];  
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

UITextField * text1 = [alert textFieldAtIndex:0];

UITextField * text2 = [alert textFieldAtIndex:1];

text1.keyboardType = UIKeyboardTypeNumberPad;

text2.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

[alert show];

 

    

UIAlertViewStyleSecureTextInput和UIAlertViewStylePlainTextInput可以通過textFieldIndex為0來獲取輸入框對象。

UIAlertViewStyleLoginAndPasswordInput可以通過textFieldIndex為0和1分別獲取用戶名輸入框對象和密碼輸入框對象。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM