IOS开发(1)之UIAlertView

本文介绍了iOS开发中UIAlertView控件的基本用法,包括普通弹框、代理弹框及带输入框的弹框等不同应用场景,并提供了代码示例。

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

1.前言

之前简单的学习了Objective-C的基础语法,从今天起我们开始学习简单的IOS视图开发。

2.UIAlertView入门

2.1普通弹框

使用提示视图的最好方法,当然是使用特定的初始化方法: 

[plain]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.       
  6.     //Title:这个字符串会显示在提示视图的最上面的Title。  
  7.     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"  
  8.     //message:这是要给用户看的实际讯息。  
  9.     message:@"Message"  
  10.     //delegate:我们可以传递委托对象(可选)给提示视图。当视图状态变更时,委托对象会被通知。传递的参数对象必须实践UIAlertViewDelegate协定.  
  11.     delegate:nil  
  12.     //cancelButtonTitle:可选参数。这个字符串符会显示在提示视图的取消按钮上。通常有取消按钮的提示视图都是要要求用户做确认,用户若不愿意进行该动作,就会按下取消。这个按钮的的标是可以自行设定的,不一定会显示取消。  
  13.     cancelButtonTitle:@"Cancel"  
  14.     //otherButtonTitles:可选参数。若你希望提示视图出现其他按钮,只要传递标题参数。此参数需用逗号分隔,用 nil 做结尾。  
  15.     otherButtonTitles:@"Ok", nil];  
  16.     [alertView show];  
  17. }  

运行结果:



2.2代理弹框

.h文件:

[plain]  view plain copy
  1. @interface ZYAlertYesOrNoViewController : UIViewController<UIAlertViewDelegate>//增加UIAlertViewDelegate代理  
  2. -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;  
  3. @end  
.m文件:

[plain]  view plain copy
  1. - (void)viewDidAppear:(BOOL)animated{  
  2.     [super viewDidAppear:animated];  
  3.     //初始化UIAlertView  
  4.     self.view.backgroundColor = [UIColor whiteColor];  
  5.     UIAlertView *alertView = [[UIAlertView alloc]  
  6.                               initWithTitle:@"Rating"  
  7.                               message:@"Can you please rate our app?"  
  8.                               //为自身添加代理  
  9.                               delegate:self  
  10.                               cancelButtonTitle:[self noButtonTitle]  
  11.                               otherButtonTitles:[self yesButtonTitle], nil];  
  12.     [alertView show];  
  13. }  

[plain]  view plain copy
  1. - (NSString *) yesButtonTitle{ return @"Yes";  
  2. }  
  3. - (NSString *) noButtonTitle{ return @"No";  
  4. }  
  5. //判断用户按下的是Yes还是No  
  6. -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{  
  7.         NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];  
  8.         if ([buttonTitle isEqualToString:[self yesButtonTitle]]) {  
  9.            NSLog(@"User pressed the Yes button.");  
  10.         }else if([buttonTitle isEqualToString:[self noButtonTitle]]){  
  11.             NSLog(@"User pressed the No button.");  
  12.         }  
  13. }  

当点击Yes按钮后
运行结果(控制台显示):

2013-04-22 11:21:33.675 UIAlertViewTestDemo[1147:c07] User pressed the Yes button.


2.3带输入框的Alert

[plain]  view plain copy
  1. //登陆弹出框:一个文本输入框,一个密码框  
  2. UIAlertView *alertView = [[UIAlertView alloc]  
  3.                               initWithTitle:@"Password" message:@"Please enter your credentials" delegate:self  
  4.                               cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];  
  5. //设置AlertView的样式  
  6. [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];  
  7. [alertView show];  

运行结果:

UIAlertView样式:

[plain]  view plain copy
  1. type enum{  
  2. UIAlertViewStyleDefalut=0;//默认样式  
  3. UIAlertViewStyleSecureTextInput,//密码框  
  4. UIAlertViewStylePlainTextInput,//文本输入框  
  5. UIAlertViewStyleLoginAndPasswordInput //有登陆效果的提示框  
  6. }UIAlertViewStyle  

3.结语

对于UIAlertView控件,就介绍这么多了,希望对大家有所帮助。

Demo例子下载地址:http://download.youkuaiyun.com/detail/u010013695/5286596

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值