欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

IOS开发(1)之UIAlertView

来源: 开发者 投稿于  被查看 32803 次 评论:163

IOS开发(1)之UIAlertView


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

2.UIAlertView入门
2.1普通弹框
使用提示视图的最好方法,当然是使用特定的初始化方法:

 

[plain]
- (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]; 

- (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文件:


[plain]
@interface ZYAlertYesOrNoViewController : UIViewController<UIAlertViewDelegate>//增加UIAlertViewDelegate代理 
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; 
@end 

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


[plain]
- (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]; 

- (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];
}
[plain]

- (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."); 
        } 

- (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


[plain]
//登陆弹出框:一个文本输入框,一个密码框 
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 *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Password" message:@"Please enter your credentials" delegate:self
                              cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
//设置AlertView的样式
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[alertView show];
运行结果:

 

\

 


UIAlertView样式:


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

相关文章

    暂无相关文章

用户评论