首先,视图控制器必须得实现协议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. 判断DataReader中是否有指定列

    取出的DataReader如果在读取过程中报没有列的错误可以用这个方法. //调用该方法判断datareader中是否有指定列 public static bool readerExists(IDat ...

  2. 洛谷 P1508 Likecloud-吃、吃、吃

    P1508 Likecloud-吃.吃.吃 题目提供者JosephZheng 标签 动态规划 难度 普及/提高- 题目背景 问世间,青春期为何物? 答曰:"甲亢,甲亢,再甲亢:挨饿,挨饿,再 ...

  3. Linux 获取文件夹下的所有文件

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4129616.html #include <string> #include &l ...

  4. winfrom 水晶按钮

    闲来无事,从网上找了不少自定义控件,然后整理了一下,做了一个水晶按钮 /// <summary> /// 表示 Windows 的按钮控 /// </summary> [Des ...

  5. angularJs--$on、$emit和$broadcast的使用

    $emit只能向parent controller传递event与data $broadcast只能向child controller传递event与data $on用于接收event与data 例子 ...

  6. PhpExcel数组输出到Excel浏览器下载

    经常是mysql查出二维数组,并且数组的带key也是有意义的,考虑到经常用,就打算弄个函数出来,方便以后用! 相对是规范的数组哈,具体可看下$data数组: 生成的excel第一行是对应的key: 直 ...

  7. Mysql 存储过程小例子

    创建存储过程: DELIMITER $$ USE `database_name`$$ DROP PROCEDURE IF EXISTS `add_or_update_user`$$ )) BEGIN ...

  8. nrm —— 快速切换 NPM 源 (附带测速功能)

    以前我们介绍过cnpmjs.org和最近推出的淘宝 npm 两个 NPM 镜像.除此之外,还有一些国外的 NPM 镜像.不同地区访问不同的镜像速度可能有差异,然后各个镜像各自都可能有少数包暂时没有同步 ...

  9. iphone开发第一个UI应用程序QQ

    #import <UIKit/UIKit.h> @interface HViewController : UIViewController @property (retain, nonat ...

  10. K最近邻算法

    K最近邻(K-Nearest-Neighbour,KNN)算法是机器学习里简单易掌握的一个算法.通过你的邻居判断你的类型,“近朱者赤,近墨者黑”表达了K近邻的算法思想. 一.算法描述: 1.1 KNN ...