UIALertView与UIAlertViewDelegate的基本用法
首先,视图控制器必须得实现协议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的基本用法的更多相关文章
- iOS开发之UIAlertView与UIAlertController的详尽用法说明
本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用 ...
- IOS开发:UIAlertView使用
链接地址:http://www.2cto.com/kf/201307/231841.html UIAlertView是什么就不介绍了 1.基本用法 1 UIAlertView *view = [[UI ...
- 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50163725 一. 分段控件 (UISegmentedControl) 控件展 ...
- iOS引用当前显示的UIAlertView
UIAlertView在iOS里和一般的UIView不一样,有时候使用起来会有一些不便.特别要引用当前显示的UIAlertView的时候,就存在一些难度. 在iOS7以前,可以下面的代码可以解决这个问 ...
- Objective-C Runtime之着魔的UIAlertView
前言: 上篇文章写的是Runtime的一个入门教程,刚哥问我那个Associated Objects加回调是啥时候用,那我就来告诉你啦!我们在使用UIAlertView的时候用的多. 传统的UIAle ...
- iOS 8及以后版本 如何创建UIAlertView?
1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...
- iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建
1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...
- UIAlertView+Blocks.h
#import <Foundation/Foundation.h> typedef void (^DismissBlock)(int buttonIndex); typedef void ...
- iOS-UI-UI控件概述
以下列举一些在开发中可能用得上的UI控件: IBAction和IBOutlet,UIView 1 @interface ViewController : UIViewController 2 3 @p ...
随机推荐
- 单链表(Single Linked List)
链表的结点结构 ┌───┬───┐ │data|next│ └───┴───┘ data域--存放结点值的数据域 next域--存放结点的直接后继的地址(位置)的指针域(链域) 实例:从终端输入 ...
- ios9基础知识(UI)总结
UIWindow.UILabel.UIColor.UIScreen.UIViewController.UIView.UIControl.UIButton.IBOutlet.IBAction.UISte ...
- 创建一个基本的 Win32 窗口
#include <Windows.h> //Forward declarations bool InitMainWindow(HINSTANCE, int); LRESULT CALLB ...
- linux常用命令--diff
diff是Unix系统的一个很重要的工具程序. 它用来比较两个文本文件的差异,是代码版本管理的基石之一.你在命令行下,输入: $ diff <变动前的文件> <变动后的文件> ...
- C语言-getopt函数
#include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...
- DOM事件一览表
type Bubbling phase Cancelable Target node types DOM interface DOMActivate Yes Yes Element UIEvent D ...
- has leaked ServiceConnection com.baidu.location.LocationClient
02-06 05:01:52.806: E/ActivityThread(1120): Activity com.project.xxxActivity $1@45fc5af8 that was or ...
- 【ecos学习5】redboot 加载运行hello world
背景: 从主机 192.168.2.14 IP,下载bin文件hello到ecos. redboot>load -v -h 192.168.2.14 hello Using default pr ...
- python 调用shell或windows命令
目前使用到的有两种方式:os.system()和os.popen() import os os.system('cd e:cjx') #调用后返回执行结果状态 如果想获得返回结果,则使用以下方式: i ...
- 【关于360极速浏览器的xx极速模式自动切换到兼容模式】
原理上是可以的. 1 360基于Chromium 开源浏览器内核,它本身就是一个壳子.. 2 7.0之后的极速浏览器,不支持 它官方的那个声明标记.<meta name=”renderer” ...