UIAlertView的使用方法
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"这是一个简单的警告框!"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
2. 为UIAlertView添加多个按钮
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"请选择一个按钮:"
delegate:nil
cancelButtonTitle:@"取消"
otherButtonTitles:@"按钮一", @"按钮二", @"按钮三",nil];
[alert show];
[alert release];
3. 如何判断用户点击的按钮
UIAlertView有一个委托UIAlertViewDelegate ,继承该委托来实现点击事件
头文件:
@interface MyAlertViewViewController : UIViewController<UIAlertViewDelegate> {
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
-(IBAction) buttonPressed;
@end
源文件:
-(IBAction) buttonPressed
{
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"请选择一个按钮:"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"按钮一", @"按钮二", @"按钮三",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
点击“取消”,“按钮一”,“按钮二”,“按钮三”的索引buttonIndex分别是0,1,2,3
4. 手动的取消对话框
[alertdismissWithClickedButtonIndex:0 animated:YES];
5:为UIAlertView添加子视图
在为UIAlertView对象太添加子视图的过程中,有点是需要注意的地方,如果删除按钮,也就是取消UIAlerView视图中所有的按钮的时候,可能会导致整个显示结构失衡。按钮占用的空间不会消失,我们也可以理解为这些按钮没有真正的删除,仅仅是他不可见了而已。如果在UIAlertview对象中仅仅用来显示文本,那么,可以在消息的开头添加换行符(@"\n)有助于平衡按钮底部和顶部的空间。
下面的代码用来演示如何为UIAlertview对象添加子视图的方法。
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"请等待"
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[alert show];
UIActivityIndicatorView*activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activeView.center = CGPointMake(alert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f);
[activeView startAnimating];
[alert addSubview:activeView];
[activeView release];
[alert release];
6. UIAlertView默认情况下所有的text是居中对齐的。 那如果需要将文本向左对齐或者添加其他控件比如输入框时该怎么办呢? 不用担心, iPhone SDK还是很灵活的, 有很多delegate消息供调用程序使用。 所要做的就是在
- (void)willPresentAlertView:(UIAlertView *)alertView
中按照自己的需要修改或添加即可, 比如需要将消息文本左对齐,下面的代码即可实现:
-(void) willPresentAlertView:(UIAlertView *)alertView
{
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment=UITextAlignmentLeft;
}
}
}
这段代码很简单, 就是在消息框即将弹出时,遍历所有消息框对象,将其文本对齐属性修改为 UITextAlignmentLeft即可。
添加其他部件也如出一辙, 如下代码添加两个UITextField:
-(void) willPresentAlertView:(UIAlertView *)alertView
{
CGRect frame = alertView.frame;
frame.origin.y -= 120;
frame.size.height += 80;
alertView.frame = frame;
for( UIView * viewin alertView.subviews )
{
if( ![viewisKindOfClass:[UILabelclass]] )
{
CGRect btnFrame = view.frame;
btnFrame.origin.y += 70;
view.frame = btnFrame;
}
}
UITextField* accoutName = [[UITextFieldalloc] init];
UITextField* accoutPassword = [[UITextFieldalloc] init];
accoutName.frame = CGRectMake( 10, frame.origin.y + 40,frame.size.width - 20, 30 );
accoutPassword.frame = CGRectMake( 10, frame.origin.y + 80,frame.size.width -20, 30 );
accoutName.placeholder = @"请输入账号";
accoutPassword.placeholder = @"请输入密码";
accoutPassword.secureTextEntry = YES;
[alertView addSubview:accoutPassword];
[alertView addSubview:accoutName];
[accoutName release];
[accoutPassword release];
}
显示将消息框固有的button和label移位, 不然添加的text field会将其遮盖住。 然后添加需要的部件到相应的位置即可。
对于UIActionSheet其实也是一样的, 在
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
中做同样的处理一样可以得到自己想要的界面。
标签:
iosuialertviewit |
分类: IOS |
举例:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
标准的双按钮,cancel那个buttonIndex 为0, ok button 的buttonIndex为1
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“ThirdButton”, nil];
和程序里的顺序一样,cancel ok thirdButton 的buttonIndex 分别为0 1 2
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"message:@"Defalut" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@“ThirdButton”, nil];
同理,cancel ok thirdButton FourthButton的buttonIndex 分别为0 1 2 3
[alertView show];
UIAlertView Delegate
UIAlertView的使用方法的更多相关文章
- IOS 警告框 (UIAlertView)的使用方法
1.普通警告框 IOS的SDK中提供了一个方便的类库UIAlertView,配合着不同参数来使用此类可以做出大多数的警告框,如下代码是IOS最简单的警告框. UIAlertView *alert = ...
- iOS开发之UIAlertView与UIAlertController的详尽用法说明
本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用 ...
- iOS - UIAlertView
前言 NS_CLASS_DEPRECATED_IOS(2_0, 9_0, "UIAlertView is deprecated. Use UIAlertController with a p ...
- iOS的APP验证版本更新方法
一个老项目没有集成自动检测及提示用户更新新版本的功能,自己在网上查资料捣鼓出自己的方法,可能比较粗陋,希望大家多多指导,我们的项目的版本号是X.X.X的格式,所以直接把字符串转换成float类型的值无 ...
- Objective-C Runtime之着魔的UIAlertView
前言: 上篇文章写的是Runtime的一个入门教程,刚哥问我那个Associated Objects加回调是啥时候用,那我就来告诉你啦!我们在使用UIAlertView的时候用的多. 传统的UIAle ...
- IOS--UIAlertView的使用方法详细
IOS--UIAlertView的使用方法详细 // UIAlertView的常用方法 // 标准样式 UIAlertView *oneAlertView = [[UIAlertView allo ...
- 【IOS 开发】基本 UI 控件详解 (UISegmentedControl | UIImageView | UIProgressView | UISlider | UIAlertView )
转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50163725 一. 分段控件 (UISegmentedControl) 控件展 ...
- iOS中清除缓存的方法 以及SDWebimage自带的清除缓存方法
1 SDWebimage中 (1) 计算缓存的大小 单位 : (MB) CGFloat size = [[SDImageCache sharedImageCache] getSize] / 102 ...
- UIAlertView和UIAlertController
UIAlertView 随着苹果上次iOS 5的发布,对话框视图样式出现在了我们面前,直到现在它都没有发生过很大的变化.下面的代码片段展示了如何初始化和显示一个带有“取消”和“好的”按钮的对话框视图. ...
随机推荐
- React Native专题
转载注明出处:地址:http://www.lcode.org本文出自:[江清清的技术专栏]本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶 ...
- Unity3d个人信息开发流程
1.首先先对需要交互的属性进行G/S,比如声明金币的属性 private int _coin; public String Coin{ get{ return _coin; } set{ return ...
- webservice使用基本技巧
一,webService基本概念 webService也叫XMLWeb SerVice WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独 ...
- CKEditor&ckfindtor
前言 之前的项目中一直使用的是FCKeditor,昨天突然有个想法:为什么不试一下新的CKEditor呢?于是花了大半天的时间去学习它的使用方法,如今把我的学习过程与大家分享一下. 谈起FCKedit ...
- Protobuf的自动反射消息类型的方法
1. 每个消息头部中带上type name,作为消息的类型标识 2. 通过type name可以找到描述符Descriptor*, FindMessageTypeByName 3. 通过描述符Desc ...
- Android 开源项目 eoe 社区 Android 客户端(转)
本文内容 环境 开源项目 eoe 社区 Android 客户端 本文介绍 eoe 社区 Android 客户端.它是一个开源项目,功能相对简单,采用侧边菜单栏.可以学习一下.点击此处查看 GitHub ...
- android中的文件操作详解以及内部存储和外部存储(转载)
原文链接:http://m.blog.csdn.net/article/details?id=17725989 摘要 其实安卓文件的操作和java在pc环境下的操作并无二致,之所以需要单独讲解是因为安 ...
- Jquery分步学习一
<script type="text/javascript" src="../js/jquery-1.11.0.js"></script> ...
- dede当前位置各种写法
方法一.Dedecms当前位置{dede:field name='position'/} 方法二.dede:field name='position' runphp='yes'} $ ...
- 网页嵌入百度地图和使用百度地图api自定义地图的详细步骤
在网页中插入百度地图 如果想在自己的网页上面加入百度地图的话,可以用百度地图的api.具体使用方法如下: 第一步:进入百度创建地图的网站http://api.map.baidu.com/lbsapi/ ...