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

具体代码如下:

ViewController.h中的代码如下:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIAlertViewDelegate>

@end

ViewController.m中的详细代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib //初始化AlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"
message:@"message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OtherBtn",nil];
//设置标题与信息,通常在使用frame初始化AlertView时使用
alert.title = @"AlertViewTitle";
alert.message = @"AlertViewMessage"; //这个属性继承自UIView,当一个视图中有多个AlertView时,可以用这个属性来区分
alert.tag = 0;
//只读属性,看AlertView是否可见
NSLog(@"%d",alert.visible);
//通过给定标题添加按钮
[alert addButtonWithTitle:@"addButton"];
//按钮总数
NSLog(@"number Of Buttons :%d",alert.numberOfButtons);
//获取指定索引的按钮标题
NSLog(@"buttonTitleAtIndex1:%@",[alert buttonTitleAtIndex:1]);
NSLog(@"buttonTitleAtIndex2:%@",[alert buttonTitleAtIndex:2]);
//获取取消按钮的索引
NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);
//获取第一个其他按钮的索引
NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);
//显示AlertView
[alert show];
[alert release];
} #pragma marks -- UIAlertViewDelegate --
//根据被点击按钮的索引处理点击事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"clickButtonAtIndex:%d",buttonIndex);
} //AlertView已经消失时执行的事件
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"didDismissWithButtonIndex");
} //ALertView即将消失时的事件
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"willDismissWithButtonIndex");
} //AlertView的取消按钮的事件
-(void)alertViewCancel:(UIAlertView *)alertView
{
NSLog(@"alertViewCancel");
} //AlertView已经显示时的事件
-(void)didPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"didPresentAlertView");
} //AlertView即将显示时
-(void)willPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"willPresentAlertView");
} - (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

UIALertView与UIAlertViewDelegate的基本用法的更多相关文章

  1. iOS开发之UIAlertView与UIAlertController的详尽用法说明

    本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用 ...

  2. IOS开发:UIAlertView使用

    链接地址:http://www.2cto.com/kf/201307/231841.html UIAlertView是什么就不介绍了 1.基本用法 1 UIAlertView *view = [[UI ...

  3. 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )

    转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50163725 一. 分段控件 (UISegmentedControl) 控件展 ...

  4. iOS引用当前显示的UIAlertView

    UIAlertView在iOS里和一般的UIView不一样,有时候使用起来会有一些不便.特别要引用当前显示的UIAlertView的时候,就存在一些难度. 在iOS7以前,可以下面的代码可以解决这个问 ...

  5. Objective-C Runtime之着魔的UIAlertView

    前言: 上篇文章写的是Runtime的一个入门教程,刚哥问我那个Associated Objects加回调是啥时候用,那我就来告诉你啦!我们在使用UIAlertView的时候用的多. 传统的UIAle ...

  6. iOS 8及以后版本 如何创建UIAlertView?

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  7. iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  8. UIAlertView+Blocks.h

    #import <Foundation/Foundation.h> typedef void (^DismissBlock)(int buttonIndex); typedef void ...

  9. iOS-UI-UI控件概述

    以下列举一些在开发中可能用得上的UI控件: IBAction和IBOutlet,UIView 1 @interface ViewController : UIViewController 2 3 @p ...

随机推荐

  1. 宏定义 define

    #define kOut -1 用一个字符串代替一个数据 用kOut表示-1(一般开头有一个小写的k) 作用: 1.为了让一些数据有意义 #define kUseId asdjlfdjafa #def ...

  2. 世界国家名与英文名【json】

    英文版 var geolocation= [   ["AO", "Angola"],   ["AF", "Afghanistan& ...

  3. Ubuntu安装tftp服务器

    一.安装如下软件包: sudo apt-get install xinetd tftpd tftp 二.在/etc/xinetd.d/目录下创建tftp文件,并输入如下内容. 执行命令:sudo vi ...

  4. jQuery + css 公告从左往右滚动

    $(function() { // 公告滚动 $(".notice-content").textScroll(); }); /** * 从右往左滚动文字 * @returns {u ...

  5. IE9以下通过css让html页面背景图片铺满整个屏幕

    第一种方法不设为背景图片,通过css来控制样式,可兼容到IE6,代码如下: <!DOCTYPE html> <html lang="en"> <hea ...

  6. UVA 120 Stacks of Flapjacks

    每次从最底部开始处理,如果不是最大值,则把最大值翻到底部.这就是最优解.原理自己模拟一下就好... 注意半径不是从1开始.数据处理要仔细. #include <iostream> #inc ...

  7. MVC4 Controller器同名问题

    一.创建项目 二.修改配置文件(路由器) 详情如下: 总结:解决Controller器同名问题,只需要修改2外,App_Start里的RouteConfig.cs文件和Area下的***AreaReg ...

  8. VS2012常用快捷建(必备)

    1. 强迫智能感知:Ctrl+J:2.强迫智能感知显示参数信息:Ctrl-Shift-空格:3.格式化整个块:Ctrl+K+F4. 检查括号匹配(在左右括号间切换): Ctrl +]5. 选中从光标起 ...

  9. python学习day4

    目录 一.迭代器 二.yield生成器 三.装饰器 四.递归 五.基础算法 迭代器 #1.在不使用for循环的情况下 li = [11 ,22, 33, 44] #count = len(li) #s ...

  10. jquery 单击table行事件和radio的选中事件冲突

    原文地址:http://zhidao.baidu.com/link?url=HER7lu4jqejWUhWQO2nq6LZ6tf7vyhPZRADSL-xaBQSF4P4yftD9vg08Ss8HF- ...