IOS开发(1)之UIAlertView

来源:互联网 发布:ae mac 中文 编辑:程序博客网 时间:2024/04/28 22:19

1.前言

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

2.UIAlertView入门

2.1普通弹框

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

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

运行结果:



2.2代理弹框

.h文件:

@interface ZYAlertYesOrNoViewController : UIViewController<UIAlertViewDelegate>//增加UIAlertViewDelegate代理-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;@end
.m文件:

- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    //初始化UIAlertView    self.view.backgroundColor = [UIColor whiteColor];    UIAlertView *alertView = [[UIAlertView alloc]                              initWithTitle:@"Rating"                              message:@"Can you please rate our app?"                              //为自身添加代理                              delegate:self                              cancelButtonTitle:[self noButtonTitle]                              otherButtonTitles:[self yesButtonTitle], nil];    [alertView show];}

- (NSString *) yesButtonTitle{ return @"Yes";}- (NSString *) noButtonTitle{ return @"No";}//判断用户按下的是Yes还是No-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{        NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];        if ([buttonTitle isEqualToString:[self yesButtonTitle]]) {           NSLog(@"User pressed the Yes button.");        }else if([buttonTitle isEqualToString:[self noButtonTitle]]){            NSLog(@"User pressed the No button.");        }}

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

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


2.3带输入框的Alert

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

运行结果:

UIAlertView样式:

type enum{UIAlertViewStyleDefalut=0;//默认样式UIAlertViewStyleSecureTextInput,//密码框UIAlertViewStylePlainTextInput,//文本输入框UIAlertViewStyleLoginAndPasswordInput //有登陆效果的提示框}UIAlertViewStyle

3.结语

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

Demo例子下载地址:http://download.csdn.net/detail/u010013695/5286596





原创粉丝点击