UIAlertView使用详解

Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox();

UIAlertView 继承自 UIView (@interface UIAlertView : UIView )

一、简单的初始化一个UIAlertView 对象。

UIAlertView* alert = [[UIAlertView alloc] init];

激活 alert ,让它显示。

[alert show];

结果将如下:

这样虽然出现了一个提示框,但是太不过友好,让人根本无法使用。

二,带有button的提示框。

UIAlertView 里面包含了另外一种用来初始化的方法。

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

带有一个button。

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"简单的提示框" message:@"simple alert" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];

[alert show];

带有多个button

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"简单的提示框" message:@"simple alert" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Button1",@"Button2",@"Button3" ,nil];

[alert show];

要想处理多个button点击之后做出不同响应,那么必须让当前控制器类遵循 UIAlertViewDelegate 协议。

UIAlertViewDelegate 里面包含了一个方法(- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;),用来触发在点击button之后的操作,判断是哪一个button 有两种方式,一种是更具button 的索引,

另外一种是button的title。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

NSString* buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];

if([buttonTitle isEqualToString:@"Button1"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"Button2"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"Button3"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"ok"]){

userOutput.text = buttonTitle;

}

}

三 、给提示框添加输入框,最经典的案例,appstore 下载的时候输入密码、

首先,初始化UITextField

userInput = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 70.0, 260.0, 25.0)];

[userInput setBackgroundColor:[UIColor whiteColor ]];

将userInput  添加在 alert上,

[alert addSubview:userInput];

- (IBAction)btnWithTextField:(id)sender {

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Please Enter Your Email Address" message:@"simple alert" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];

userInput = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 70.0, 260.0, 25.0)];

[userInput setBackgroundColor:[UIColor whiteColor ]];

[alert addSubview:userInput];

[alert show];

[alert release];

[userInput release];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

NSMutableString* buttonTitle = [NSMutableString stringWithString:[alertView buttonTitleAtIndex:buttonIndex]];

if([buttonTitle isEqualToString:@"Button1"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"Button2"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"Button3"]){

userOutput.text = buttonTitle;

}if([buttonTitle isEqualToString:@"ok"]){

[buttonTitle appendString:userInput.text];

userOutput.text = buttonTitle;

}

}

Objective-C ,ios,iphone开发基础:UIAlertView使用详解的更多相关文章

  1. [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解

    UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...

  2. Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)

    使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...

  3. Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录

    第一步:下载第三方库,点击 连接 下载, 第二部:准备数据库:按照连接&中博客的步骤实现数据库, 数据库的设计大致如下表: id        username             pas ...

  4. Objective-C ,ios,iphone开发基础:几个常用类-NSNumber

    2013-08-21 在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objectiv ...

  5. Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器

    新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView  两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...

  6. Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)

    json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...

  7. Objective-C ,ios,iphone开发基础:http网络编程

    - (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...

  8. Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器

    第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...

  9. Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作

    SQLite  是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...

随机推荐

  1. tensorflow-gpu

    1.Cuda 查看Cuda支持的GPU型号:https://developer.nvidia.com/cuda-gpus 下载cuda:https://developer.nvidia.com/cud ...

  2. NodeJS学习笔记 (21)事件机制-events(ok)

    模块概览 events模块是node的核心模块之一,几乎所有常用的node模块都继承了events模块,比如http.fs等. 模块本身非常简单,API虽然也不少,但常用的就那么几个,这里举几个简单例 ...

  3. Conservative GC (Part two :MostlyCopyingGC )

    目录 MostlyCopyingGC 概要 堆结构 分配 new_obj()函数 add_pages()函数 GC执行过程 mostly_copying()函数 promote_page()函数 pa ...

  4. tt

    Oracle报错处理 1.oem启动报错 解决方案:

  5. CSU 1378 Shipura 简单模拟

    上周末中南的题,当时就知道是个简单模拟题,可是一个多小时就是没写出来,代码能力啊 >.< 题意: 某人发明了一种程序语言,只支持一种运算">>",和一种函数 ...

  6. ArcGIS api for javascript——地图配置-定制缩放动画,定制缩放框

    描述 本例展示了当用户放大或缩小地图时如何定义地图的动画.zoomDuration和zoomRate是Dojo动画属性,他们确定了动画的duration和帧刷新的rate .这些属性单位是毫秒,zoo ...

  7. 学习笔记 Java_静态_继承 2014.7.12

    一.静态 1.构造函数:       特点:       1. 函数名和类名同样.       2. 不用定义返回值类型(和void不是一回事,而构造函数是根本不用定义返回值类型).       3. ...

  8. 【HeadFirst设计模式——开篇】

    近期在看HeadFirst,接下来的一段时间会陆续更新有关HeadFirst设计模式相关的文章.记得非常久之前在学习大话设计模式的时候,仅仅是走马观花的大致走过一遍.至于里面非常多东西都掌握的不是非常 ...

  9. matlab 常用机器学习算法的实现

    1. KNN 分类 load fisheriris X = meas; Y = species; % 3 分类问题 % 通过训练集进行训练 Mdl = fitcknn(X, Y, 'NumNeighb ...

  10. Redis封装值ZSet

    /// <summary> /// Sorted Sets是将 Set 中的元素增加了一个权重参数 score,使得集合中的元素能够按 score 进行有序排列 /// 1.带有权重的元素 ...