原文网址: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. Windows 进程通信 之 DDE技术

    DDE (Dynamic Data Exchange,DDE)动态数据交换,是一种进程间通信机制,它最早是随着Windows由微软提出的.当前大部分软件仍旧支持DDE,但最近十年里微软已经停止发展DD ...

  2. CentOS 6.6安装LAMP和Subversion服务器

    目标:在CentOS 6.6上安装LAMP,并安装最新版1.8.*的Subversion服务器,和Subversion权限管理前端iF.svnadmin. 安装步骤: 安装新一些版本LAMP步骤 1. ...

  3. linux配置mysql,tomcat命令vi

    [root@yangchehome bin]# ./mysqld_safe -user=mysql & [1] 17135 [root@yangchehome bin]# 140316 16: ...

  4. EF提供的三种查询方式

    這邊簡單介紹一下,ADO.Net Entity Framework 提供的三種查詢方式, Linq to Entities Query Builder Mothed Entity SQL Langua ...

  5. 一个Java程序员应该掌握的10项技能

    1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...

  6. Ubuntu环境下Hadoop1.2.1, HBase0.94.25, nutch2.2.1各个配置文件一览

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  7. Android学习及开发随记1:Android Studio安装配置

    1.本系列仅为个人使用,概不负责.随着时间推移,部分内容可能因为软件更新而出现不能对应的情况. 本文的配置情况,仅针对Android Studio v1.1.0 windows版本 全新安装. Goo ...

  8. spring aop通过joinpoint传递参数

    三.总结. 我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得. 三.总结. 我们可以通过Advice中添加一个JoinPoint ...

  9. IP地址字符串与BigInteger的转换

    /**  * Copyright (c) 2010, 新浪网支付中心  *      All rights reserved.  *  * Java IP地址字符串与BigInteger的转换,  * ...

  10. NSDictionary 初始化

    NSDictionary *dic1=[NSDictionary dictionaryWithObject:@"1" forKey:@"a"];         ...