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

具体代码如下:

ViewController.h中的代码如下:

  1. #import <UIKit/UIKit.h>
  2. @interface ViewController : UIViewController<UIAlertViewDelegate>
  3. @end

ViewController.m中的详细代码:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view from its nib
  5. //初始化AlertView
  6. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"
  7. message:@"message"
  8. delegate:self
  9. cancelButtonTitle:@"Cancel"
  10. otherButtonTitles:@"OtherBtn",nil];
  11. //设置标题与信息,通常在使用frame初始化AlertView时使用
  12. alert.title = @"AlertViewTitle";
  13. alert.message = @"AlertViewMessage";
  14. //这个属性继承自UIView,当一个视图中有多个AlertView时,可以用这个属性来区分
  15. alert.tag = 0;
  16. //只读属性,看AlertView是否可见
  17. NSLog(@"%d",alert.visible);
  18. //通过给定标题添加按钮
  19. [alert addButtonWithTitle:@"addButton"];
  20. //按钮总数
  21. NSLog(@"number Of Buttons :%d",alert.numberOfButtons);
  22. //获取指定索引的按钮标题
  23. NSLog(@"buttonTitleAtIndex1:%@",[alert buttonTitleAtIndex:1]);
  24. NSLog(@"buttonTitleAtIndex2:%@",[alert buttonTitleAtIndex:2]);
  25. //获取取消按钮的索引
  26. NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);
  27. //获取第一个其他按钮的索引
  28. NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);
  29. //显示AlertView
  30. [alert show];
  31. [alert release];
  32. }
  33. #pragma marks -- UIAlertViewDelegate --
  34. //根据被点击按钮的索引处理点击事件
  35. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  36. {
  37. NSLog(@"clickButtonAtIndex:%d",buttonIndex);
  38. }
  39. //AlertView已经消失时执行的事件
  40. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
  41. {
  42. NSLog(@"didDismissWithButtonIndex");
  43. }
  44. //ALertView即将消失时的事件
  45. -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
  46. {
  47. NSLog(@"willDismissWithButtonIndex");
  48. }
  49. //AlertView的取消按钮的事件
  50. -(void)alertViewCancel:(UIAlertView *)alertView
  51. {
  52. NSLog(@"alertViewCancel");
  53. }
  54. //AlertView已经显示时的事件
  55. -(void)didPresentAlertView:(UIAlertView *)alertView
  56. {
  57. NSLog(@"didPresentAlertView");
  58. }
  59. //AlertView即将显示时
  60. -(void)willPresentAlertView:(UIAlertView *)alertView
  61. {
  62. NSLog(@"willPresentAlertView");
  63. }
  64. - (void)viewDidUnload
  65. {
  66. [super viewDidUnload];
  67. // Release any retained subviews of the main view.
  68. // e.g. self.myOutlet = nil;
  69. }

UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法的更多相关文章

  1. 【转】 UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

    原文网址:http://blog.csdn.net/enuola/article/details/7900346 首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定d ...

  2. (转) UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

    首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件. 具体代码如下: #import <UIK ...

  3. 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法

    在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法 最近在做一个小东西,使用kindeditor上传图片的时候,自己写了一个上传的方法,按照协议规则通过ajax返回json ...

  4. 【NX二次开发】修改dlx对话框标题的方法

    修改dlx名称, 修改对话框标题的方法: theDialog->TopBlock()->FindBlock("Dialog")->GetProperties()- ...

  5. Swift - 告警框(UIAlertView)的用法

    1,下面代码创建并弹出一个告警框,并带有“取消”“确定”两个按钮 (注:自IOS8起,建议使用UIAlertController) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...

  6. JQuery EasyUI 对话框的使用方法

    下面看一下EasyUI的对话框效果图 js代码: 复制代码代码如下: <script language="javascript" type="text/javasc ...

  7. Delphi的WebBrowser改造,对网页中Alter等对话框的改造方法(通过COM来改造)

    刚有一段时间没做博客了,今天刚好有人问了这个问题,而自己以前也弄过,于是这里有了一篇新的博文. 关于改造WebBrowser控件的一些技巧,大家可以参考MSDN或者蒋晟写的一个东西,里面有讲的很详细的 ...

  8. Eclipse用法:自动生成get和set方法

      方法一 Java的类中,除了常量声明为静态且公有的,一般的对象数据作用域,都是声明为私有的.这样做能保护对象的属性不会被随意改变,调试的时候也会方便很多:在类的公有方法中大一个调用栈就能看到哪里改 ...

  9. adb环境配置+常用adb命令+Logcat命令的用法+手动进行文件比对的方法+批量挪bug

    1. adb环境配置:下载adb环境变量包:打开计算机属性-高级系统设置-环境变量:新建变量adb,值为刚才的环境变量包路径:编辑path值,在最后面加上;%adb%;确定就妥了 2. 常用adb命令 ...

随机推荐

  1. Xml 学习

    XML概述 XML的历史背景 GML(1969):通用标记语言 IBM公司的一些专家们一起研究的一个课题: 软件必须有高度的可移植性,而且必须有一个统一的标准.为了实现软件的一处编写多处运行这个愿景所 ...

  2. 将一个应用程序添加做成windows服务

    需求来源: 在服务器端运行的一个应用程序,为完成客户端路径分析等功能: 此应用程序只需要运行着就可以,没有界面等要求: 因此,考虑把此应用程序添加到服务器端电脑管理的服务中,可以启动和停止. 这里添加 ...

  3. will VS be going to

    1.Future - Will vs. Going to 2."Will be doing" vs. "will do" 3.Simple Future

  4. WiFi安全之WPA介绍

    WPA,全称为Wi-Fi Protected Access,是一种保护WiFi安全的系统,实现了IEEE 802.11i的大部分标准,是一种替代WEP的过渡方案. 这个协议包含了前向兼容RC4的加密协 ...

  5. Mysql配置文件my.cnf解析

    # vim /etc/my.cnf [client] port = 3306 //客户端所连接的端口号 socket = /tmp/mysql.sock //客户端所连接的sock文件存放位置 [my ...

  6. Visual Studio 2015和.Net 2015 预览版在线安装和ISO镜像安装光盘下载

    微软刚刚宣布了 Visual Studio 2015和.Net 2015 预览版,并同时提供了下载. 微软在纽约正进行中的#Connect# 全球开发者在线大会上宣布了Visual Studio 20 ...

  7. AngularJS(7)-表格

    ng-repeat 指令可以完美的显示表格. <!DOCTYPE html> <html lang="en"> <head> <meta ...

  8. php判断服务器是否支持Gzip压缩功能

    Gzip可以压缩网页大小从而达到加速打开网页的速度,目前主流的浏览器几乎都支持这个功能,但开启Gzip是需要服务器支持的,在这里我们简单的使用php来判断服务器是否支持Gzip功能. 新建一个php类 ...

  9. 符合web标准的网页下拉菜单

    <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> < ...

  10. maven项目转eclipse工程的命令:eclipse.bat

    call mvn clean:clean call mvn eclipse:eclipse -DdownloadSources=true @pause 复制以上内容,保存为eclipse.bat 以后 ...