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. Ubuntu下配置修改IP地址

    一.使用命令设置Ubuntu IP地址 1.修改配置文件blacklist.conf禁用IPV6:sudo vi /etc/modprobe.d/blacklist.conf 2.在文档最后添加 bl ...

  2. BaseAdapter导致notifyDataSetChanged()无效的四个原因及处理方法

    前一段时间在做一个项目的时候遇到了一个关于BaseAdapter的notifyDataSetChanged()方法无效问题,当时在网上搜了一个解决方法,今天又遇到了一个类似的问题,我在这里做个记录,防 ...

  3. ZIOZIA_百度百科

    ZIOZIA_百度百科 ZIOZIA

  4. 【Maven】运行项目,报ClassNotFound错误

    我们在pom.xml中添加了那些依赖的jar后,一旦在代码中应用.当我们本地调试时,会报ClassNotFound错误,这是为什么?? 因为Tomcat不懂这些配置,你必须把Maven Depende ...

  5. 在VS上配置OpenCV

    这几篇帖子讲的挺仔细的,而且不坑,结合看看就没问题了~~ http://www.cnblogs.com/cuteshongshong/p/4057193.html http://my.phirobot ...

  6. WebView之2

    首先需要添加权限: <uses-permission android:name="android.permission.INTERNET"/> MainActivity ...

  7. 我的wifi

    首先利用百度查找 怎么承载网络,托管网络的用户名和密码 . 1.以管理员身份运行命令提示符: 快捷键win+R→输入cmd→回车 2.启用并设定虚拟WiFi网卡: 运行命令:netsh wlan se ...

  8. get get_children方法

    get 方法: [root@wx03 zook]# cat a4.pl use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; my $zk = ...

  9. 凡客副总裁被曝离职:或因IPO受阻|凡客|王春焕|离职_互联网_新浪科技_新浪网

    凡客副总裁被曝离职:或因IPO受阻|凡客|王春焕|离职_互联网_新浪科技_新浪网 凡客副总裁被曝离职:或因IPO受阻 2013年05月07日 00:56   每日经济新闻    我有话说     每经 ...

  10. [置顶] 使用红孩儿工具箱完成基于Cocos2d-x的简单游戏动画界面

    [Cocos2d-x相关教程来源于红孩儿的游戏编程之路CSDN博客地址:http://blog.csdn.net/honghaier 红孩儿Cocos2d-X学习园地QQ3群:205100149,47 ...