当前位置:编程学习 > wap >>

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";
}
//判断用户

补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,