UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

简介: <p style="color:rgb(54,46,43); font-family:Arial; font-size:14px; line-height:26px"> 首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件。</p> <p style="color:rgb(54,46,43

首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件。

具体代码如下:

ViewController.h中的代码如下:

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIAlertViewDelegate>  
  4.   
  5. @end  


ViewController.m中的详细代码:

[java]  view plain copy
  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. }
目录
相关文章
|
5月前
|
Python
pyqt 重写关闭窗口事件代码
pyqt 重写关闭窗口事件代码
39 0
|
11月前
|
JavaScript 前端开发
JavaScript 点击事件:一个按钮触发另一个按钮
给按钮 2 添加点击事件 click 触发按钮 1 的点击事件,就算给按钮 1 添加样式 display: none; 或 visibility: hidden; 隐藏起来也能触发。
547 0
|
JavaScript
element-ui中下拉command传递多参数事件封装
element-ui中下拉command传递多参数事件封装
448 0
element-ui中下拉command传递多参数事件封装
|
JavaScript 前端开发 API
如何使用原生的 JavaScript 代码,触发 SAP UI5 按钮控件的点击事件处理函数
如何使用原生的 JavaScript 代码,触发 SAP UI5 按钮控件的点击事件处理函数
如何使用原生的 JavaScript 代码,触发 SAP UI5 按钮控件的点击事件处理函数
|
C#
WPF,强制捕获鼠标事件,鼠标移出控件外依然可以执行强制捕获的鼠标事件
原文:WPF,强制捕获鼠标事件,鼠标移出控件外依然可以执行强制捕获的鼠标事件 在WPF中,只有鼠标位置在某个控件上的时候才会触发该控件的鼠标事件。例如,有两个控件都注册了MouseDown和MouseUp事件,在控件1上按下鼠标,不要放开,移动到控件2上再放开。
2246 0
|
JavaScript C#
C#(WPF)去除事件中注册的事件处理方法!
在WPF中,移除一个事件中已经注册的处理方法,看似简单,实际还是很痛苦的一件事情。因为C#的灵活性,定义事件的方法也是多种多样。我自己定义了一个事件: public event EventHandler TestEvent; 当我想注销这个事件上注册的所有方法的时候,我可以按如下的方法进行 Delegate[] dels = TestEvent.
3928 0
|
Web App开发 前端开发 JavaScript