[置顶] 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 ...
 - [置顶] 提高生产力:Web开发基础平台WebCommon的设计和实现
		
Web开发中,存在着各种各样的重复性的工作.为了提高开发效率,不在当码农,我在思考和实践如何搭建一个Web开发的基础平台. Web开发基础平台的目标和功能 1.提供一套基础的开发环境,整合了常用的框架 ...
 - [置顶] Objective-C ,ios,iphone开发基础:protocol 协议(委托,代理)的声明
		
协议是为了弥补Objective-c中类只能单继承的缺陷,在Objective-c2.0之前当一个类遵循一个协议的时候,必须在类中实现协议的所有方法,在Objective-c2.0之后协议中的方法就有 ...
 - [置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)
		
在c++中我们可以多继承来实现代码复用和封装使程序更加简练.在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(catego ...
 - [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,
		
在x-code Version 4.3.2 (4E2002)下编译: 在 Controller. m 文件下添加如下实例方法即可: - (void)viewDidUnload { [super vie ...
 - [置顶] Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton
		
第一步:新建一个工程,在 .h文件中坐如下声明: #import <UIKit/UIKit.h> @interface MyButtonViewController : UIViewCon ...
 - [置顶] Objective-C ,/,ios,/iphone开发基础:协议(protocol)
		
protocol协议时为了补充Objective-C 只能单继承的缺陷而增加的一个新功能.Objective-C重所有的方法都是虚方法,所以在oc重也就没有关键字 virtual一说,有了协议可以补充 ...
 - [置顶] Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
		
SQLite 是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...
 - [置顶] Objective-C ,ios,iphone开发基础:命名规范
		
命名规范:http://bukkake.iteye.com/blog/695492 点击打开链接
 
随机推荐
- shell学习之字符串处理
			
1.获取字符串长度 value=abcd;echo ${#value} 输出:42.获取子串 value=abcd;:} 输出:d value:a:b,其中a表示起始位置,b表示获取的子串的长度. 3 ...
 - Identity-修改Error错误提示为中文
			
第一步:重写IdentityErrorDescriber public class CustomIdentityErrorDescriber : IdentityErrorDescriber ...
 - VS QT 配置OpenGL
			
在visual studio 下编译OpenGL代码出现以下错误,原因是vs没有自带opengl库,需要自己引入 无法解析的外部符号 __imp__glClear@4 无法解析的外部符号 __imp_ ...
 - 搜狐畅游:每月给员工直系长辈2000元爱孝薪_企业新闻_265G产业频道
			
搜狐畅游:每月给员工直系长辈2000元爱孝薪_企业新闻_265G产业频道 搜狐畅游:每月给员工直系长辈2000元爱孝薪
 - nginx日志每日定时写入Hdfs
			
#!/bin/bash hadoop_home=/opt/hadoop-2.4.0 tw_nginx_log_file=/home/chiline.com.all/access_com_tw.log ...
 - JS - 删除确认
			
<a href="javascript:if(confirm('确实要删除吗?'))location='<{:U('Admin/Update/deleteuserinfo', a ...
 - BZOJ 1833: [ZJOI2010]count 数字计数( dp )
			
dp(i, j, k)表示共i位, 最高位是j, 数字k出现次数. 预处理出来. 差分答案, 对于0~x的答案, 从低位到高位进行讨论 -------------------------------- ...
 - 树莓派安装ftp服务器
			
在树莓派安装ftp服务器,可上载\下载文件 vsftpd是开源的轻量级的常用ftp服务器. 1,安装vsftpd服务器 (约400KB)sudo apt-get install vsftpd 2,启动 ...
 - jquery倒计时自动跳转
			
刚开始我用下面这种方法一直报错,不知是什么原因,就是多加了页面加载时调用这个方法,还请高手看到后小小留言解惑
 - JVM -- CMS
			
并发的标记—清除(Concurrent Mark Sweep,缩写为 CMS)收集器,使得在整个收集的过程中只是很短的暂停应用的执行,可通过在 JVM 参数中设置-XX:UseConcMarkSwee ...