链接地址:http://www.cnblogs.com/scandy-yuan/archive/2013/03/11/2954194.html

1. 最简单的用法

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 

                                                  message:@"这是一个简单的警告框!" 

                                                  delegate:nil   

                                                  cancelButtonTitle:@"确定" 

                                                  otherButtonTitles:nil];  

[alert show];  

2. 为UIAlertView添加多个按钮

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 

                                                  message:@"请选择一个按钮:" 

                                                  delegate:nil   

                                                  cancelButtonTitle:@"取消" 

                                                  otherButtonTitles:@"按钮一",
@"按钮二",
@"按钮三", nil]
[alert show];

3. 如何判断用户点击的按钮

UIAlertView有一个委托UIAlertViewDelegate ,继承该委托来实现点击事件

头文件:

@interface MyAlertViewViewController : UIViewController<UIAlertViewDelegate> {

}

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

-(IBAction) buttonPressed;

@end

源文件:

-(IBAction) buttonPressed

{

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 

                                                 message:@"请选择一个按钮:" 

                                                 delegate:self   

                                                 cancelButtonTitle:@"取消" 

                                                 otherButtonTitles:@"按钮一", @"按钮二", @"按钮三",nil];  

[alert show];  

}

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

{

NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];

UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                 message:msg

                                                 delegate:nil

                                                 cancelButtonTitle:@"确定"

                                                 otherButtonTitles:nil];

[alert show];

}

点击“取消”,“按钮一”,“按钮二”,“按钮三”的索引buttonIndex分别是0,1,2,3

4.修改提示框样式

iOS5中UIAlertView新增了一个属性alertViewStyle,它的类型是UIAlertViewStyle,是一个枚举值:

typedef enum {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle;

alertViewStyle属性默认是UIAlertViewStyleDefault。我们可以把它设置为UIAlertViewStylePlainTextInput,那么AlertView就显示为这样:

UIAlertViewStyleSecureTextInput显示为:

UIAlertViewStyleLoginAndPasswordInput为:

当然我们也可以通过创建UITextField来关联这里的输入框并设置键盘响应的样式

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"CD-KEY" 

                                                 message:@"please enter cd-key:" 

                                                 delegate:self   

                                                 cancelButtonTitle:@"Cancel" 

                                                 otherButtonTitles:@"Ok",nil];
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; UITextField * text1 = [alert textFieldAtIndex:0]; UITextField * text2 = [alert textFieldAtIndex:1]; text1.keyboardType = UIKeyboardTypeNumberPad; text2.keyboardType = UIKeyboardTypeNumbersAndPunctuation; [alert show];

    

UIAlertViewStyleSecureTextInput和UIAlertViewStylePlainTextInput可以通过textFieldIndex为0来获取输入框对象。

UIAlertViewStyleLoginAndPasswordInput可以通过textFieldIndex为0和1分别获取用户名输入框对象和密码输入框对象。

UIAlertView笔记的更多相关文章

  1. ios/mac/COCOA系列 -- UIALertVIew 学习笔记

    最近在学习ios开发,学习的书籍<ios7 Pragramming cookbook>,做笔记的目的以后方便查看.笔记形式是小例子,将书上的例子书写完整. UIAlertViewClass ...

  2. 【代码笔记】iOS-带输入框的UIAlertView

    一,效果图. 二,代码. //点击任何处,弹出输入框 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UIAlertV ...

  3. RAC学习笔记

    RAC学习笔记 ReactiveCocoa(简称为RAC),是由Github开源的一个应用于iOS和OS开发的新框架,Cocoa是苹果整套框架的简称,因此很多苹果框架喜欢以Cocoa结尾. 在学习Re ...

  4. xmpp整理笔记:发送图片信息和声音信息

    图片和音频文件发送的基本思路就是: 先将图片转化成二进制文件,然后将二进制文件进行base64编码,编码后成字符串.在即将发送的message内添加一个子节点,节点的stringValue(节点的值) ...

  5. xmpp整理笔记:用户网络连接及好友的管理

    xmpp中的用户连接模块包括用户的上线与下线信息展现,用户登录,用户的注册: 好友模块包括好友的添加,好友的删除,好友列表的展示. 在xmpp中 负责数据传输的类是xmppStream,开发的过程中, ...

  6. IOS笔记 1

    < ![CDATA[ 笔记 UIWindows 与UIView的关系iOS的坐标系统视图层次结构视图坐标(Frame和Bounds区别)UIView的常用属性和方法坐标系统的变换UIView内容 ...

  7. Effective Objective-C 读书笔记

    一本不错的书,给出了52条建议来优化程序的性能,对初学者有不错的指导作用,但是对高级阶段的程序员可能帮助不是很大.这里贴出部分笔记: 第2条: 使用#improt导入头文件会把头文件的内容全部暴露到目 ...

  8. 【Swift】iOS开发笔记(二)

    前言 这个系列主要是一些开发中遇到的坑记录分享,有助于初学者跨过这些坑,攒够 7 条发一篇. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯 ...

  9. WWDC 2014 Session笔记 - iOS界面开发的大一统

    本文是我的 WWDC 2014 笔记 中的一篇,涉及的 Session 有 What's New in Cocoa Touch Building Adaptive Apps with UIKit Wh ...

随机推荐

  1. The Use of Aliases in ElasticSearch

    http://paulsabou.com/blog/2012/04/15/the-use-of-aliases-in-elasticsearch/ https://github.com/taskrab ...

  2. QPointer很大程度上避免了野指针(使用if语句判断即可,类似于dynamic_cast),而且使用非常方便 good

    QPointer 如何翻译呢?我不太清楚,保留英文吧. The QPointer class is a template class that provides guarded pointers    ...

  3. java selenium webdriver实战 helloWord

    第一步:建立Maven项目 Selenium 支持 maven 工程,这会让你的工作更加简便. 用 Eclipse 建个 Maven 的工程,建成后,修改 pom.xml <dependenci ...

  4. Android实现 ScrollView + ListView无滚动条滚动

    Android实现 ScrollView+ListView无滚动条滚动,即ListView的数据会全部显示完,但Listview无滚动条. 核心代码如下: 1. NoScrollListView.ja ...

  5. Cocos2d-X 动作展示《一》

    因为Cocos2d-X中的动作较多,我将全部的动作制作成了一个滚动视图.每一个滚动视图上都有动作名,单击滚动视图就能够展示对应的动作 程序效果图: 使用滚动视图实现动作切换 动作展示 程序代码: 首先 ...

  6. linux中时间函数

    linux下常用时间类型有四种: time_t . struct   tm. struct  timeval .    struct   timespec 1.time_t   时间函数 time_t ...

  7. Css Rest 方法

    在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是 重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈现相同的 ...

  8. Lowest Common Ancestor of a Binary Tree, with Parent Pointer

    Given a binary tree, find the lowest common ancestor of two given nodes in tree. Each node contains ...

  9. jbpmAPI-5

    Part II. jBPM Core 5.1.概述本章介绍了API需要加载过程和执行它们.更多的细节如何定义过程本身,看看在BPMN 2.0章.与流程引擎交互(例如,开始一个过程),你需要建立一个会话 ...

  10. JS 利用window.open实现post方式的参数传递

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...