UIAlertView用法

本文介绍UIAlertView的基本用法,包括创建简单的警告框、添加多个按钮及如何响应按钮点击事件。此外,还详细讲解了如何通过代理模式获取点击的按钮索引,并展示了如何自定义UIAlertView的样式,如添加子视图、调整文本对齐方式等。

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

1. 最简单的用法

UIAlertView*alert =[[UIAlertView alloc]initWithTitle:@"提示" 

                                          message:@"这是一个简单的警告框!" 

                                         delegate:nil  

                                          cancelButtonTitle:@"确定" 

                                          otherButtonTitles:nil]; 

[alert show];  

[alert release]; 

UIAlertView用法

 

2. 为UIAlertView添加多个按钮

UIAlertView*alert =[[UIAlertView alloc]initWithTitle:@"提示" 

                                          message:@"请选择一个按钮:" 

                                          delegate:nil  

                                          cancelButtonTitle:@"取消" 

                                          otherButtonTitles:@"按钮一",@"按钮二",@"按钮三",nil]; 

[alert show];  

[alert release]; 

UIAlertView用法

 3. 如何判断用户点击的按钮

UIAlertView有一个委托UIAlertViewDelegate ,继承该委托来实现点击事件

 头文件:

@interface MyAlertViewViewController :UIViewController<UIAlertViewDelegate>{

}

- (void)alertView:(UIAlertView*)alertViewclickedButtonAtIndex:(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*)alertViewclickedButtonAtIndex:(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];  

UIAlertView用法

 6. UIAlertView默认情况下所有的text是居中对齐的。那如果需要将文本向左对齐或者添加其他控件比如输入框时该怎么办呢? 不用担心, iPhone SDK还是很灵活的,有很多delegate消息供调用程序使用。 所要做的就是在

- (void)willPresentAlertView:(UIAlertView*)alertView

中按照自己的需要修改或添加即可, 比如需要将消息文本左对齐,下面的代码即可实现:

-(void)willPresentAlertView:(UIAlertView *)alertView

{

     for(UIView *view inalertView.subviews )

     {

         if([view isKindOfClass:[UILabel class]])

          {

              UILabel*label = (UILabel*) view;

              label.textAlignment=UITextAlignmentLeft;

          }

     }

}

 UIAlertView用法

 这段代码很简单,就是在消息框即将弹出时,遍历所有消息框对象,将其文本对齐属性修改为 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移位, 不然添加的textfield会将其遮盖住。 然后添加需要的部件到相应的位置即可。

对于UIActionSheet其实也是一样的, 在
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
中做同样的处理一样可以得到自己想要的界面。


资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 华为移动服务(Huawei Mobile Services,简称 HMS)是一个全面开放的移动服务生态系统,为企业和开发者提供了丰富的工具和 API,助力他们构建、运营和推广应用。其中,HMS Scankit 是华为推出的一款扫描服务 SDK,支持快速集成到安卓应用中,能够提供高效且稳定的二维码和条形码扫描功能,适用于商品扫码、支付验证、信息获取等多种场景。 集成 HMS Scankit SDK 主要包括以下步骤:首先,在项目的 build.gradle 文件中添加 HMS Core 库和 Scankit 依赖;其次,在 AndroidManifest.xml 文件中添加相机访问和互联网访问权限;然后,在应用程序的 onCreate 方法中调用 HmsClient 进行初始化;接着,可以选择自定义扫描界面或使用 Scankit 提供的默认扫描界面;最后,实现 ScanCallback 接口以处理扫描成功和失败的回调。 HMS Scankit 内部集成了开源的 Zxing(Zebra Crossing)库,这是一个功能强大的条码和二维码处理库,提供了解码、生成、解析等多种功能,既可以单独使用,也可以与其他扫描框架结合使用。在 HMS Scankit 中,Zxing 经过优化,以更好地适应华为设备,从而提升扫描性能。 通常,ScanKitDemoGuide 包含了集成 HMS Scankit 的示例代码,涵盖扫描界面的布局、扫描操作的启动和停止以及扫描结果的处理等内容。开发者可以参考这些代码,快速掌握在自己的应用中实现扫码功能的方法。例如,启动扫描的方法如下: 处理扫描结果的回调如下: HMS Scankit 支持所有安卓手机,但在华为设备上能够提供最佳性能和体验,因为它针对华为硬件进行了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值