UIAlertView笔记

链接地址: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分别获取用户名输入框对象和密码输入框对象。

时间: 2024-08-11 01:41:20

UIAlertView笔记的相关文章

【iOS开发每日小笔记(四)】iOS 7中如何除去UIAlertView 规避delegate对象销毁后接收消息的crash

这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.该分类的文章,内容涉及的知识点可能是很简单的.或是用很短代码片段就能实现的,但在我看来它们可能会给用户体验.代码效率得到一些提升,或是之前自己没有接触过的技术,很开心的学到了,放在这里得瑟一下.其实,90%的作用是帮助自己回顾.记忆.复习.如果看官觉得太easy,太碎片,则可以有两个选择:1,移步[iOS探究]分类,对那里的文章进行斧正:2,在本文的评论

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

最近在学习ios开发,学习的书籍<ios7 Pragramming cookbook>,做笔记的目的以后方便查看.笔记形式是小例子,将书上的例子书写完整. UIAlertViewClass 的使用场景 1,向用户以一个警告的形式显示信息. 2,让用户确认一些动作 3,让用户输入用户名和密码 4,让用户输入一些文本,这些文本在程序被使用 例1   实现简单显示一些警告信息 新建一个 Single View Application 简单工程,工程名字维AlterView,扩展前缀CB   代码如下

【代码笔记】带输入框的UIAlertView

一,效果图. 二,代码. //点击任何处,弹出输入框 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"请输入分组名称" message:@"\n" delegate:self cancelButtonTitle:@"添加" otherButtonTi

学习笔记--两个UIView之间跳转、tag,UIAlertView基本使用

一.两个UIView之间的跳转 //在.h文件中定义全局view_1.view_2 -(void)toView1{ if(!view_2){ [view_2 removeFromSuperview]; } view_1 = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)]; [self.window addSubview:view_1]; UIImageView *imageView_1 = [[UIImageView alloc]ini

IOS开发学习笔记-(2)键盘控制,键盘类型设置,alert 对话框

一.关闭键盘,放弃第一响应者,处理思路有两种 ① 使用文本框的 Did End on Exit 绑定事件 ② UIControl on Touch 事件 都去操作 sender 的  resignFirstResponder #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *txtUserName; @pro

Effective Objective-C 读书笔记

一本不错的书,给出了52条建议来优化程序的性能,对初学者有不错的指导作用,但是对高级阶段的程序员可能帮助不是很大.这里贴出部分笔记: 第2条: 使用#improt导入头文件会把头文件的内容全部暴露到目标文件中,而且如果两个类之间存在循环引用则会出现编译错误,所以要尽量使用@class进行类声明. 如果需要实现一个协议,则必须#improt这个协议的头文件,所以可以将协议单独定义在一个.h文件当中.如果这个协议是代理模式协议的一部分,即需要与类捆绑使用才有实际意义,则建议定义在类当中,并以类名为前

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

本文是我的 WWDC 2014 笔记 中的一篇,涉及的 Session 有 What's New in Cocoa Touch Building Adaptive Apps with UIKit What's New in Interface Builder View Controller Advancements in iOS 8 A Look Inside Presentation Controllers iOS 8 和 OS X 10.10 中一个被强调了多次的主题就是大一统,Apple

IOS开发学习笔记-(3) 进度条、等待动画开始停止

一.创建对应空间视图  ,如下图: 二.编写对应的 .h 代码,如下 : #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activWaitNetWork; @property (weak, nonatomic) IBOutlet UIProgressView *pgrsDownLo

iphone开发-基础笔记总结(1)

1.ios完整学习路线 2.ios开发的一般步骤: 搭建界面                                         UI界面(User Interface) 发送网络请求                                   多线程/网络 网络数据解析                                   json/xml解析 在界面上进行数据展示                   数据的封装展示 3.为了方便开发者开发出强大的功能,苹果提