首先,视图控制器必须得实现协议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. jquery mobile 入门

    简介:jQuery Mobile框架可以轻松的帮助我们实现非常好看的.可跨设备的Web应用程序.我们将后续的介绍中向大家介绍大量的代码及实例. jQuery一直以来都是非常流行的富客户端及Web应用程 ...

  2. CAA调试

    在需要调试的Module(*.m)上右键,选择属性,命令位置选择你的framework目录    路径选择对应工程目录下的\intel_a(或者Win64    --    64位机器) 然后就可以尽 ...

  3. iphone iOS7恢复到iOS6教程

    步骤一:首先根据您iOS设备型号,下载最新的iOS 6固件,您可以进入苹果官网下载,也可以以下网址下载. http://sj.zol.com.cn/ios613/

  4. android 手势滑动

    1.概述, 两次都是画曲线统计图用到手势滑动.左滑动,右滑动曲线图翻页 2.直接上代码 3.注: 第一次使用的时候是implement了 OnTouchListener 接口,是在画图布局上layou ...

  5. 将一段含有0的字符数组赋给string

    string有个成员函数,assign() 可以这样: string str; str.assign(temp, sizeof(temp));

  6. Inno Setup 精灵显示插件 InnoFairy (V2.0 版本)

    原文 http://restools.hanzify.org/article.asp?id=111 一个如影随形的小精灵会令到你的安装程序更加人性化. 就是这样一个功能的Inno Setup插件, 希 ...

  7. linux系统怎么改为中文版(转)

    linux系统安装好后怎么改为中文版呢?今天就跟大家介绍下linux系统改为中文版的方法,希望能帮助到大家! 以下是linux系统改为中文版的四种方法,一起来看看: 方法1:写入环境变量 echo & ...

  8. HTML 5 <details> 标签

    <details> 标签用于描述文档或文档某个部分的细节. <details> <summary>Copyright 2011.</summary> & ...

  9. 一个简单java爬虫爬取网页中邮箱并保存

    此代码为一十分简单网络爬虫,仅供娱乐之用. java代码如下: package tool; import java.io.BufferedReader; import java.io.File; im ...

  10. linux C之access函数 (20

    http://blog.sina.com.cn/s/blog_6a1837e90100uh5d.html linux C之access函数 (20access():判断是否具有存取文件的权限 相关函数 ...