UIAlierView方法

首先,視圖控制器必須得實現協議UIAlertViewDelegate中的方法,並指定delegate爲self,才能使彈出的Alert窗口響應點擊事件。

如果使用多個AlertView的話,可以使用tag值來判斷。

具體代碼如下:

 

ViewController.h中的代碼如下:

 

  1. #import   
  2.   
  3. @interface ViewController : UIViewController  
  4.   
  5. @end  


 

 

ViewController.m中的詳細代碼:

 

[java] view plaincopy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib  
  5.       
  6.     //初始化AlertView  
  7.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"  
  8.                                                    message:@"message"  
  9.                                                   delegate:self  
  10.                                          cancelButtonTitle:@"Cancel"  
  11.                                          otherButtonTitles:@"OtherBtn",nil];  
  12.     //設置標題與信息,通常在使用frame初始化AlertView時使用  
  13.     alert.title = @"AlertViewTitle";  
  14.     alert.message = @"AlertViewMessage";  
  15.       
  16.     //這個屬性繼承自UIView,當一個視圖中有多個AlertView時,可以用這個屬性來區分  
  17.     alert.tag = 0;  
  18.     //只讀屬性,看AlertView是否可見  
  19.     NSLog(@"%d",alert.visible);  
  20.     //通過給定標題添加按鈕  
  21.     [alert addButtonWithTitle:@"addButton"];  
  22.     //按鈕總數  
  23.     NSLog(@"number Of Buttons :%d",alert.numberOfButtons);  
  24.     //獲取指定索引的按鈕標題  
  25.     NSLog(@"buttonTitleAtIndex1:%@",[alert buttonTitleAtIndex:1]);  
  26.     NSLog(@"buttonTitleAtIndex2:%@",[alert buttonTitleAtIndex:2]);  
  27.     //獲取取消按鈕的索引  
  28.     NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);  
  29.     //獲取第一個其他按鈕的索引  
  30.     NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);  
  31.     //顯示AlertView  
  32.     [alert show];  
  33.     [alert release];  
  34. }  
  35.   
  36. #pragma marks -- UIAlertViewDelegate --  
  37. //根據被點擊按鈕的索引處理點擊事件  
  38. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  39. {  
  40.     NSLog(@"clickButtonAtIndex:%d",buttonIndex);  
  41. }  
  42.   
  43. //AlertView已經消失時執行的事件  
  44. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  
  45. {  
  46.     NSLog(@"didDismissWithButtonIndex");  
  47. }  
  48.   
  49. //ALertView即將消失時的事件  
  50. -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex  
  51. {  
  52.     NSLog(@"willDismissWithButtonIndex");  
  53. }  
  54.   
  55. //AlertView的取消按鈕的事件  
  56. -(void)alertViewCancel:(UIAlertView *)alertView  
  57. {  
  58.     NSLog(@"alertViewCancel");  
  59. }  
  60.   
  61. //AlertView已經顯示時的事件  
  62. -(void)didPresentAlertView:(UIAlertView *)alertView  
  63. {  
  64.     NSLog(@"didPresentAlertView");  
  65. }  
  66.   
  67. //AlertView即將顯示時  
  68. -(void)willPresentAlertView:(UIAlertView *)alertView  
  69. {  
  70.     NSLog(@"willPresentAlertView");  
  71. }  
  72.   
  73. - (void)viewDidUnload  
  74. {  
  75.     [super viewDidUnload];  
  76.     // Release any retained subviews of the main view.  
  77.     // e.g. self.myOutlet = nil;  
  78. }  
發佈了33 篇原創文章 · 獲贊 5 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章