1. 最簡單的用法

 

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

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

                                                  delegate:nil   

                                                  cancelButtonTitle:@"確定" 

                                                  otherButtonTitles:nil];  

[alert show];  

 

螢幕快照 2012-11-13 上午12.34.03  

 

2. 為UIAlertView添加多個按鈕

 

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

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

                                                  delegate:nil   

                                                  cancelButtonTitle:@"取消" 

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

[alert show];  

 

螢幕快照 2012-11-13 上午12.35.05  

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

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

  .h 檔:

 

@interface MyAlertViewViewController : UIViewController<UIAlertViewDelegate> {

}

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

-(IBAction) buttonPressed;

@end

 .m 檔:

 

-(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. 手動的取消對話框

 

[alertdismissWithClickedButtonIndex:0 animated:YES];

 

 

 

arrow
arrow

    shenfive 發表在 痞客邦 留言(0) 人氣()