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. [置顶] 提高生产力:Web开发基础平台WebCommon的设计和实现

    Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...

  3. [置顶] Objective-C ,ios,iphone开发基础:protocol 协议(委托,代理)的声明

    协议是为了弥补Objective-c中类只能单继承的缺陷,在Objective-c2.0之前当一个类遵循一个协议的时候,必须在类中实现协议的所有方法,在Objective-c2.0之后协议中的方法就有 ...

  4. [置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)

    在c++中我们可以多继承来实现代码复用和封装使程序更加简练.在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(catego ...

  5. [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,

    在x-code Version 4.3.2 (4E2002)下编译: 在 Controller. m 文件下添加如下实例方法即可: - (void)viewDidUnload { [super vie ...

  6. [置顶] Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton

    第一步:新建一个工程,在 .h文件中坐如下声明: #import <UIKit/UIKit.h> @interface MyButtonViewController : UIViewCon ...

  7. [置顶] Objective-C ,/,ios,/iphone开发基础:协议(protocol)

    protocol协议时为了补充Objective-C 只能单继承的缺陷而增加的一个新功能.Objective-C重所有的方法都是虚方法,所以在oc重也就没有关键字 virtual一说,有了协议可以补充 ...

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

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

  9. [置顶] Objective-C ,ios,iphone开发基础:命名规范

    命名规范:http://bukkake.iteye.com/blog/695492  点击打开链接

随机推荐

  1. border-radius.htc为ie6-8实现圆角

    ~~圆角是比较常用的css3属性,但是ie6-8并不支持圆角,可用border-radius.htc  html组件实现圆角, border-radius.htc内部应用vml进行了重绘 border ...

  2. 一个ShellExecute的超全说明(www.phidels.com这个网站很多内容)

    I. Introduction / Déclaration ShellExecute fait partie des innombrables fonctions de l'API Windows ; ...

  3. 爬虫框架Scrapy

    前面十章爬虫笔记陆陆续续记录了一些简单的Python爬虫知识, 用来解决简单的贴吧下载,绩点运算自然不在话下. 不过要想批量下载大量的内容,比如知乎的所有的问答,那便显得游刃不有余了点. 于是乎,爬虫 ...

  4. BZOJ 1610: [Usaco2008 Feb]Line连线游戏

    1610: [Usaco2008 Feb]Line连线游戏 Description Farmer John最近发明了一个游戏,来考验自命不凡的贝茜.游戏开始的时 候,FJ会给贝茜一块画着N (2 &l ...

  5. 包子IT面试培训

    包子IT面试培训 IT 面试不再怕,包子帮你圆个梦!

  6. CUDA samples 2.3节 用CUDA示例来创建CUDA项目

    2.3.1. Creating CUDA Projects for Windows 略 2.3.2  Creating CUDA Projects for Linux 默认的samples的安装路径 ...

  7. SQL查询各阶段的统计信息

            我们经常会遇到各种分类统计问题,须要将这些结果一次显示出来.这次老师提出的要求是我想看60分下面多少人.60~70多少人,70~80多少人.80~90多少人,90~100多少人.他们曾 ...

  8. UItexfile实时验证输入字符

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...

  9. Latex(一)公式自动编号与自动引用

    在进行latex引用时,有两种办法: 一,被动引用. 如有这样一段代码: $$ x^2+y^2= z^2.\eqno(1.1) $$ In this paper, we investigated (1 ...

  10. Spring MVC 3.x 版本使用 @ResponseBody 返回乱码

    由于万恶的Spring MVC 默认返回编码 是 ISO-8859-1, 使用如下配置, 可以将编码专为UTF-8, <bean class="org.springframework. ...