[iPhone开发之控件的使用]UIAlertView的各种属性、方法及代理的使用

来源:互联网 发布:橙光文字游戏制作软件 编辑:程序博客网 时间:2024/06/06 00:52
[c-sharp] view plaincopy
  1. #import "AlertViewTestViewController.h"  
  2. @implementation AlertViewTestViewController  
  3.   
  4. /* 
  5.  Tasks 
  6.   
  7.  Creating Alert Views 
  8.     – initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:   
  9.  Setting Properties 
  10.     delegate  property   
  11.     title  property   
  12.     message  property   
  13.     visible  property   
  14.  Configuring Buttons 
  15.     – addButtonWithTitle:   
  16.     numberOfButtons  property   
  17.     – buttonTitleAtIndex:   
  18.     cancelButtonIndex  property   
  19.     firstOtherButtonIndex  property   
  20.  Displaying 
  21.     – show   
  22.  Dismissing 
  23.     – dismissWithClickedButtonIndex:animated:  无例 
  24. */  
  25.   
  26. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  27. - (void)viewDidLoad {  
  28.     //初始化AlertView  
  29.     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"AlertViewTest"   
  30.                             message:@"message"   
  31.                             delegate:self   
  32.                             cancelButtonTitle:@"Cancel"   
  33.                             otherButtonTitles:@"OtherBtn",nil];  
  34.     //设置标题与信息,通常在使用frame初始化AlertView时使用  
  35.     alert.title = @"AlertViewTitle";  
  36.     alert.message = @"AlertViewMessage";  
  37.       
  38.     //这个属性继承自UIView,当一个视图中有多个AlertView时,可以用这个属性来区分  
  39.     alert.tag = 0;  
  40.       
  41.     //只读属性,看AlertView是否可见  
  42.     NSLog(@"%d",alert.visible);  
  43.       
  44.     //通过给定标题添加按钮  
  45.     [alert addButtonWithTitle:@"addButton"];  
  46.       
  47.     //按钮总数  
  48.     NSLog(@"numberOfButtons:%d",alert.numberOfButtons);  
  49.       
  50.     //获取指定索引的按钮的标题  
  51.     NSLog(@"buttonTitleAtIndex:%@",[alert buttonTitleAtIndex:2]);  
  52.       
  53.     //获得取消按钮的索引  
  54.     NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);  
  55.       
  56.     //获得第一个其他按钮的索引  
  57.     NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);  
  58.       
  59.     //显示AlertView  
  60.     [alert show];  
  61.       
  62.     [alert release];  
  63.     [super viewDidLoad];  
  64. }  
  65.   
  66. /* 
  67. // Override to allow orientations other than the default portrait orientation. 
  68. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
  69.     // Return YES for supported orientations 
  70.     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
  71. } 
  72. */  
  73. - (void)didReceiveMemoryWarning {  
  74.     // Releases the view if it doesn't have a superview.  
  75.     [super didReceiveMemoryWarning];  
  76.       
  77.     // Release any cached data, images, etc that aren't in use.  
  78. }  
  79. - (void)viewDidUnload {  
  80.     // Release any retained subviews of the main view.  
  81.     // e.g. self.myOutlet = nil;  
  82. }  
  83.   
  84. - (void)dealloc {  
  85.     [super dealloc];  
  86. }  
  87. #pragma mark  -- UIAlertViewDelegate --  
  88. //根据被点击按钮的索引处理点击事件  
  89. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
  90.     NSLog(@"clickedButtonAtIndex:%d",buttonIndex);  
  91. }  
  92. //AlertView已经消失时  
  93. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {  
  94.     NSLog(@"didDismissWithButtonIndex");  
  95. }  
  96. //AlertView即将消失时  
  97. - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {  
  98.     NSLog(@"willDismissWithButtonIndex");  
  99. }  
  100.   
  101. - (void)alertViewCancel:(UIAlertView *)alertView {  
  102.     NSLog(@"alertViewCancel");  
  103. }  
  104. //AlertView已经显示时  
  105. - (void)didPresentAlertView:(UIAlertView *)alertView {  
  106.     NSLog(@"didPresentAlertView");  
  107. }  
  108. //AlertView即将显示时  
  109. - (void)willPresentAlertView:(UIAlertView *)alertView {  
  110.     NSLog(@"willPresentAlertView");  
  111. }  
  112. @end  


转载http://blog.csdn.net/banyingli/article/details/6167592
原创粉丝点击