UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法

简介: <p style="color:rgb(54,46,43); font-family:Arial; font-size:14px; line-height:26px"> 首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件。</p> <p style="color:rgb(54,46,43

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

具体代码如下:

ViewController.h中的代码如下:

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIAlertViewDelegate>  
  4.   
  5. @end  


ViewController.m中的详细代码:

[java]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view from its nib  
  5.       
  6.     //初始化AlertView  
  7.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"  
  8.                                                    message:@"message"  
  9.                                                   delegate:self  
  10.                                          cancelButtonTitle:@"Cancel"  
  11.                                          otherButtonTitles:@"OtherBtn",nil];  
  12.     //设置标题与信息,通常在使用frame初始化AlertView时使用  
  13.     alert.title = @"AlertViewTitle";  
  14.     alert.message = @"AlertViewMessage";  
  15.       
  16.     //这个属性继承自UIView,当一个视图中有多个AlertView时,可以用这个属性来区分  
  17.     alert.tag = 0;  
  18.     //只读属性,看AlertView是否可见  
  19.     NSLog(@"%d",alert.visible);  
  20.     //通过给定标题添加按钮  
  21.     [alert addButtonWithTitle:@"addButton"];  
  22.     //按钮总数  
  23.     NSLog(@"number Of Buttons :%d",alert.numberOfButtons);  
  24.     //获取指定索引的按钮标题  
  25.     NSLog(@"buttonTitleAtIndex1:%@",[alert buttonTitleAtIndex:1]);  
  26.     NSLog(@"buttonTitleAtIndex2:%@",[alert buttonTitleAtIndex:2]);  
  27.     //获取取消按钮的索引  
  28.     NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);  
  29.     //获取第一个其他按钮的索引  
  30.     NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);  
  31.     //显示AlertView  
  32.     [alert show];  
  33.     [alert release];  
  34. }  
  35.   
  36. #pragma marks -- UIAlertViewDelegate --  
  37. //根据被点击按钮的索引处理点击事件  
  38. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  39. {  
  40.     NSLog(@"clickButtonAtIndex:%d",buttonIndex);  
  41. }  
  42.   
  43. //AlertView已经消失时执行的事件  
  44. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  
  45. {  
  46.     NSLog(@"didDismissWithButtonIndex");  
  47. }  
  48.   
  49. //ALertView即将消失时的事件  
  50. -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex  
  51. {  
  52.     NSLog(@"willDismissWithButtonIndex");  
  53. }  
  54.   
  55. //AlertView的取消按钮的事件  
  56. -(void)alertViewCancel:(UIAlertView *)alertView  
  57. {  
  58.     NSLog(@"alertViewCancel");  
  59. }  
  60.   
  61. //AlertView已经显示时的事件  
  62. -(void)didPresentAlertView:(UIAlertView *)alertView  
  63. {  
  64.     NSLog(@"didPresentAlertView");  
  65. }  
  66.   
  67. //AlertView即将显示时  
  68. -(void)willPresentAlertView:(UIAlertView *)alertView  
  69. {  
  70.     NSLog(@"willPresentAlertView");  
  71. }  
  72.   
  73. - (void)viewDidUnload  
  74. {  
  75.     [super viewDidUnload];  
  76.     // Release any retained subviews of the main view.  
  77.     // e.g. self.myOutlet = nil;  
  78. }
目录
相关文章
|
Web App开发 iOS开发 Windows
ios获取原生系统应用的包名
ios获取原生系统应用的包名
2951 0
|
网络协议 物联网 iOS开发
iOS - App 与外设间的通信方式
1、前言 一般 iOS 开发者做 App 开发大部分时候都是通过 Http(s) 请求跟后台服务器打交道,做一些信息展示和用户交互。很少涉及到去跟外部硬件设备连接的开发。随着近年来车联网和物联网的兴起,智能家居和智能硬件的逐步火热,越来越多的 App 被开发出来,用来跟硬件设备进行来连接,获取硬件相关信息展示或者发送指令控制硬件来提供服务。
2627 0
|
10月前
|
缓存 安全 数据挖掘
阿里云服务器目前活动中各实例规格适用场景汇总,选择指南参考
本文将基于2025年阿里云服务器相关活动的最新内容,对各个实例规格的适用场景进行详细汇总,并提供选择指南参考,帮助用户轻松选购到最适合自己的云服务器实例。
|
iOS开发
iOS UITextField(编辑框)
UITextField属性 0.enablesReturnKeyAutomatically 默认为No,如果设置为Yes,文本框中没有输入任何字符的话,右下角的返回按钮是disabled的。
1299 0
|
编解码 算法 数据格式
【经典蓝牙】蓝牙 A2DP协议分析
A2DP(Advanced Audio Distribution Profile)是蓝牙高音质音频传输协议, 用于传输单声道, 双声道音乐(一般在 A2DP 中用于 stereo 双声道) , 典型应用为蓝牙耳机。         A2DP旨在通过蓝牙连接传输高质量的立体声音频流。它使用的基本压缩算法是SBC(Sub-Band Coding)来减小音频数据的大小,同时保持高音质,SBC压缩虽然效率较低,但是是必须支持的基本备用方案。A2DP还支持其他高级编解码器,例如AAC、aptX和LDAC,这些编解码器比SBC提供更好的音质,但这些编解码器的支持取决于设备本身的支持情况。
3763 0
【经典蓝牙】蓝牙 A2DP协议分析
|
iOS开发
iOS解开.a包和.framework
解开.a包和.framework
664 0
|
JavaScript 安全 Java
autojs之一键加密
使用场景 加密autojs
1473 0
【Nest教程】数据验证class-validator
【Nest教程】数据验证class-validator
1204 0
【Nest教程】数据验证class-validator
|
算法 数据处理 数据库
TCGA数据库的利用(三)—做差异分析的三种方法
今天更新TCGA数据库的利用系列第三篇文章,在对TCGA数据进行挖掘时,通常会筛选出来一些表达量显著异常的基因,作为后续研究的对象,这个筛选过程叫做差异分析;本篇文章将分为三大模块对差异分析进行介绍
|
SQL 消息中间件 存储
大数据生态圈常用组件(二):概括介绍、功能特性、适用场景
大数据生态圈常用组件(二):概括介绍、功能特性、适用场景

热门文章

最新文章