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 ...
随机推荐
- CF732F Tourist Reform(边双联通)
题意 在一张有向图中,设 ri 为从点 i 出发能够到达的点的数量. 定义有向图的“改良值”为 ri 的最小值. 现给出一张无向图,要求给每条边定一个方向,使产生的有向图“改良值”最大. 输出 最大改 ...
- python中的future,你见过可以使用未来版本模块的语言吗?
import xxx from yy.xxx import xx from yy.xxx import xx as x python最常见的导包导模块语句 yy为包名,包就是文件夹,模块就是xxx.p ...
- [Python] Statistical analysis of time series
Global Statistics: Common seen methods as such 1. Mean 2. Median 3. Standard deviation: the larger ...
- 用react native 做的一个推酷client
用react native 做的一个推酷client 仅供大家參考.仅仅为抛砖引玉.希望大家能以此来了解react.并编写出很多其它的优质的开源库,为程序猿做出贡献. 用的的组件: Navigator ...
- Codeforces Round #313 C. Gerald's Hexagon(放三角形)
C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 免费WiFi初体验——个小白的WiFi旅程
说来羞愧,真正接触到WiFi还是在毕业后,此前自己封闭在一个人的世界,再加上外在学校的包围,我还成了个"山里"的孩子. 去年毕业了,也算是个90后,可自觉得心态过于成熟.了解外界太 ...
- shrio 加密/编码
在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...
- IBM软件技术峰会归来
为期两天在北京国际饭店会议中心的IBM软件技术峰会已近结束,此次大会最大的收获是能和沃森实验室的王博士沟通探讨人工智能软件的发展问题.领略到IBM 云计算首席架构师Jason R.McGee如何呼风唤 ...
- Unix/Linux环境下多一点不如少一点
正如很多人所知道的$PATH环境变量里存着一张目录列表,当用户要执行某一程序时,系统就会按照列表中的内容去查找该程序的位置.当程序名前不带点斜线 . / 时$PATH就会起作用. 对于普通用户和roo ...
- pythong中的全局变量的调用和嵌套函数中变量的使用
全局变量调用:想要在自定义的函数中使用全局变量,就得要在函数用关键字global声明,然后就可以对全局变量进行修改.嵌套函数中的变量的调用:要在嵌套的变量中,使用nonlocal的声明'''num = ...