原文网址:http://blog.csdn.net/enuola/article/details/7900346

首先,视图控制器必须得实现协议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对对话框的事件处理方法

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

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

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

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

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

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

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

  5. 用法:node模块都具备的方法(exports、module、require、__filename、__dirname)

    凡是玩弄nodejs的人,都明白,每一个模块都有exports.module.require.__filename.__dirname的方法 清楚了解方法的用法后,玩转node就等于清楚了日常讲话的内 ...

  6. 【Python—字典的用法】创建字典的3种方法

    #创建一个空字典 empty_dict = dict() print(empty_dict) #用**kwargs可变参数传入关键字创建字典 a = dict(one=1,two=2,three=3) ...

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

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

  8. C#委托的用法 在C#中我想在一个方法中调用另一个按钮的事件,怎样来实现?

    最开始我也不清楚,后来我是这样想了. 1.事件和委托不是一个概念,你如果是调用control的事件,可以直接在其对应的事件eventhandler上attach自己的事件方法就好了如:this.But ...

  9. 如何使用npm的部分用法以及npm被墙的解决方法

    我们要明白我们使用的npm就是node中自带的包(模块)管理工具:借助NPM可以帮助我们快速安和管理依赖包,使Node与第三方模块之间形成了一个良好的生态系统. 我们可以直接输入npm,查看帮助引导: ...

随机推荐

  1. VB程序破解之API断点[bp __vbaVarTstEq]

    软件名称:风云足彩1.7软件大小:2.1M下载地址:http://free.ys168.com/?zhinengxuanhao软件保护:注册码编写软件:Microsoft Visual Basic 5 ...

  2. Apache Options Indexes FollowSymLinks之讲解

    禁止显示Apache目录列表-Indexes FollowSymLinks 如何修改目录的配置以禁止显示 Apache 目录列表. 缺省情况下如果你在浏览器输入地址: http://localhost ...

  3. java split函数 对空的处理

    String str = "5,6,55,66,,,,@"; String[] chk_deep = str.split("@"); System.out.pr ...

  4. Graham's Scan算法

    原文链接:http://www.cnblogs.com/devymex/archive/2010/08/09/1795392.html C++/STL实现: #include <algorith ...

  5. /bin/bash^M: bad interpreter: No such file or dire

    在执行shell脚本时提示这样的错误主要是由于shell脚本文件是dos格式,即每一行结尾以\r\n来标识,而unix格式的文件行尾则以\n来标识.  查看脚本文件是dos格式还是unix格式的几种办 ...

  6. hdu 3972 1 M possible

    一般做法: 显然的超内存 #include<stdio.h> #include<algorithm> using namespace std; ],ans[]; int mai ...

  7. [topcoder]BoxesDiv2

    https://community.topcoder.com/stat?c=problem_statement&pm=13192 #include <vector> #includ ...

  8. python logging 学习笔记

    logging.basicConfig函数各参数: filename: 指定日志文件名 filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a' format: 指定输出的格 ...

  9. [Unity菜鸟] FBX模型动画提取

    角色已经人形化(Humanoid)了,那它的动画可以用在其它的模型上了也就是可以共用一套模型动画了,但是你有没有发现那动画是和fbx模型绑在一起的,没关系你可以选中这几个动画文件按Contrl+D就可 ...

  10. Easyui 中的placeholder属性

    在 easyui有文档中,没注意还真找不到placeholder属性,因为在属性只在searchbox中提到了, <input id="ss" class="eas ...