Objective-C ,ios,iphone开发基础:UIAlertView使用详解
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使用详解的更多相关文章
- [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
第一步:下载第三方库,点击 连接 下载, 第二部:准备数据库:按照连接&中博客的步骤实现数据库, 数据库的设计大致如下表: id username pas ...
- Objective-C ,ios,iphone开发基础:几个常用类-NSNumber
2013-08-21 在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objectiv ...
- Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器
新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView 两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...
- Objective-C ,ios,iphone开发基础:http网络编程
- (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...
- Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器
第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...
- Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
SQLite 是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...
随机推荐
- [HNOI2008]水平可见直线 单调栈
题目描述:在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.例如,对于直线:L1:y=x; L2:y=- ...
- 使用glPushMatrix和glPopMatrix的原因
转自 百度百科 glPushMatrix 函数将当前矩阵堆栈推送,通过一个,复制当前矩阵. 这就是后 glPushMatrix 的调用堆栈的顶部矩阵是它下面的相同的. 1. 原理讲解 终于明白 ...
- An internal error occurred during: "Checking tomcat state". Error while reading server.xml
An internal error occurred during: "Checking tomcat state". Error while reading server.xml ...
- Java,泛型类型通配符和C#对照
c#的泛型没有类型通配符,原因是.net的泛型是CLR支持的泛型,而Java的JVM并不支持泛型,仅仅是语法糖,在编译器编译的时候都转换成object类型 类型通配符在java中表示的是泛型类型的父类 ...
- vue6 发请求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- VGA接口时序约束
SF-VGA模块板载VGA显示器DA转换驱动芯片AVD7123,FPGA通过OUPLLN连接器驱动ADV7123芯片产生供给VGA显示器的色彩以及同步信号.SF-CY3核心模块与SF-VGA子模块连接 ...
- thinkphp中cookie和session中操作数组的方法
thinkphp中cookie和session中操作数组的方法 一.ThinkPHP模板中如何操作session,以及如果session中保存的是数组的情况 在ThinkPHP的模板中操作sessio ...
- Lambda表达式详细总结
(一)输入参数 在Lambda表达式中,输入参数是Lambda运算符的左边部分.它包含参数的数量可以为0.1或者多个.只有当输入参数为1时,Lambda表达式左边的一对小括弧才可以省略.输入参数的数量 ...
- Vue总结(一)
vue总结 构建用户界面的渐进式框架 渐进式:用到什么功能即可使用转么的框架子模块. 两个核心点 向应的数据绑定 当时图发生改变->自动跟新视图,利用Object.defindProperty中 ...
- Flex3中addEventListener()方法使用详解
Flex控件对象.RemoteObject等都有一个共同的方法addEventListener. 方法详细信息: 来源于:flash.events.EventDispatcher类 addEventL ...