UIAlertView用法

本文详细介绍了UIAlertView的各种使用方法,包括创建简单的警告框、添加多个按钮、判断用户点击的按钮、手动取消对话框、添加子视图及文本对齐等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIAlertView用法

原文链接:http://blog.sina.com.cn/s/blog_bf9843bf0101fahf.html

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 {

}

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

 

 

资源下载链接为: https://pan.xunlei.com/s/VOYaEvb5YbXDcdRVMg3ANOaDA1?pwd=sjwe data.py 用于创建数据集。 makelabel.py 的功能是融合数字与背景并保存。其中,一张背景图会在四个象限随机添加一个数字,且几乎无重叠。标签形状为(32,32,11),32×32 是热图输出大小,每个热图像素对应原图 4×4 的方格,每个方格作为分类器,可分出 11 类,0-9 对应数字,10 代表背景。fusion_img 函数将一个数字融合到背景图的随机位置;fusion_4img 函数考虑到单个数字太少,可处理四个数字,输入参数为(背景,(图片 1,标签 1),(图片 2,标签 2)...),输出为图片(0-255)和标签。 model.py 是模型文件,最终占用 192kb 内存。 test.py 为测试脚本,包含两个定义的函数,加载模型后可进行单张测试和视频测试,使用时注释另一个即可。onepoint 函数输入矩阵和点的 xy 坐标,逐行扫描该点周围 6 行的像素,若为 1(表示有物体),就将对应方格的 xy 加入数组并置零。扫描完周围 6 行后,若总点数超过 10 个,判定为一个物体,对所有 xy 分别求平均,得到物体中心。 单张图片后处理过程:获取输出的 32×32×11 矩阵,先扫描 32×32 区域,对每行取 argmax,若不属于背景类,说明可能存在物体,再设阈值过滤部分误识别框,然后将该点值置为 1 作为标记。 再次扫描矩阵时,为避免越界,从第 6 行开始到 25 行结束。若扫描到 1,如(20,20,3)这一格为 1,就取矩阵对应 3 的那一层(32×32 大小),将该矩阵和(20,20)坐标传入 onepoint 函数,返回中心,类别为 3。一般不会误判,若一个数字有两种可能且两种像素数都超 10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值