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. Log Collect

    http://ossectools.blogspot.com/2011/03/comprehensive-log-collection.html https://www.hacking-lab.com ...

  2. python 爬一下

    1.安装Requestswindow:pip install requestslinux:sudo pip install requests国内安装缓慢,建议到:http://www.lfd.uci. ...

  3. Xamarin.Android开发实践(一)

    原文:Xamarin.Android开发实践(一) 一.准备工作 1.创建一个空的解决方案,并命名为Phoneword 2.右击解决方案 新建->新建项目 并命名为Phoneword_Droid ...

  4. 本地机apache配置基于域名的虚拟主机详解

    1.打开apache的httpd.conf文件,找到# Virtual hosts#Include conf/extra/httpd-vhosts.conf这一段把Include conf/extra ...

  5. mac 系统开发android,真机调试解决方式(无数的坑之后吐血总结)

    近期学习android开发,安装了ADT开发环境之后,启动模拟器,慢的要死啊,全然不如苹果的好用,没法,自己买个android手机,准备联机调试程序.没想到在这个过程中,遇到了好多的坑,作为一个新人, ...

  6. ibatis3.0调用Oracle的存储过程

    直接上源码 一,oracle储存过程. create or replace procedure proc_get_th(i_hth in varchar2,o_ret out sys_refcurso ...

  7. java基础之junit测试框架

    1.导入junit包, 2.测试方法格式 public void test_*(){} 继承  TestCase  包(keep the bar green to keep the code clea ...

  8. IE6/7/8 CSS兼容性问题和解决方法汇总

    断断续续的在开发过程中收集了好多的bug以及其解决的办法,都在这个文章里面记录下来了!希望以后解决类似问题的时候能够快速解决,也希望大家能在留言里面跟进自己发现的ie6 7 8bug和解决办法! 1: ...

  9. RelativeLayout的一些感想

    1.如果只是是3-4个线性,则可以考虑RelativeLayout;多于等于5个还是尽量考虑LinearLayout好 补充:如果要把RelativeLayout作为线性布局的话,可以采用不断设置be ...

  10. WinForm - 两个窗体之间的方法调用

    方法1:   所有权法//Form1://需要有一个公共的刷新方法public   void   Refresh_Method(){ //...} //在调用Form2时,要把Form2的所有者设为F ...