UIAlertView用法

简介:

1. 最简单的用法

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
中做同样的处理一样可以得到自己想要的界面。

来源:http://hi.baidu.com/richiechyi/item/e2aa2f714996f012d0dcb39a



本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3469038.html,如需转载请自行联系原作者

目录
相关文章
|
SQL 关系型数据库 MySQL
quartz(三)任务持久化-jdbc篇(一看就会)
如果任务执行到一半突然宕机怎么办?能否重新执行任务?任务执行的数据能够以恢复吗?带着这些问题,我发现了几种持久化策略,先讲最简单,也是我们最为喜闻乐见的mysql吧!说干就干,我们来写一个最简单的吧。
343 0
|
自然语言处理 搜索推荐 算法
阿里云OpenSearch重磅推出LLM问答式搜索产品,助力企业高效构建对话式搜索服务
OpenSearch推出LLM智能问答版,面向行业搜索场景,提供企业专属问答搜索服务,基于内置的LLM大模型提供问答能力,一站式快速搭建问答搜索系统。
12902 7
|
机器学习/深度学习 算法 数据挖掘
周志华《Machine Learning》学习笔记(1)--绪论
机器学习是目前信息技术中最激动人心的方向之一,其应用已经深入到生活的各个层面且与普通人的日常生活密切相关。
270 0
周志华《Machine Learning》学习笔记(1)--绪论
&lt;&lt;突破思维障碍&gt;&gt;之思维导图
转载请注明出处:http://blog.csdn.net/horkychen *笛卡尔:普遍的怀疑第一:绝不把任何我没有明确地认识为真的东西当做的加以接受,也就是说,小心避免仓促的判断和偏见,只把那些十分清楚明白地呈现在我的心智之前,使我根本无法怀疑的东西放进我的判断之中。
1007 0
|
5天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
305 116
|
20天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
504 45
Meta SAM3开源:让图像分割,听懂你的话