UI:使用UIAlertView显示提示

1.使用􏰁示视图的最好方法,当然是使用特定的初始化方法: 
UIAlertView *alertVeiw = [[UIAlertView alloc]initWithTitle:@"Title"
                                                    message:@"alertViewDemo"
                                                      delegate:nil
                                             cancelButtonTitle:@"Cancel"
                                             otherButtonTitles:@"OK", nil];
 [alertVeiw show];

效果图:

参数说明:

title:显示Title。

message:这是要给用户看的实际讯息

delegate:我们可以传递委托对象(可选)给提示视图。当视图状态变更时,委托对象会被通知。传递的参数对象必须遵从 UIAlertViewDelegate 协议。

cancelButtonTitle:可选参数。这个字符串符会显示在􏰁示视图的取消按钮上。通常有取消按钮的提示视图都是要要求用户做确认,用户若不愿意进行该动作,就会按下取消。这个按钮的的标题是可以自行设定的,不一定是取消。具体内容取决于实际情况。

otherButtonTitles可选参数。若你希望提示视图出现其他按钮,只要传递标题参数。此参数需用逗号分 隔,用 nil 做结尾。 

2.使用代理等方法,获取用户点击的按钮等

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.view.backgroundColor = [UIColor whiteColor];
    NSString *message = @"Are you sure open this link in Safari?";
    UIAlertView *alertView = [[UIAlertView alloc]
                                                initWithTitle:@"Open Link"
                                                      message:message
                                                     delegate:self
                                            cancelButtonTitle:[self yesButtonTitle]
                                            otherButtonTitles:[self noButtonTitle], nil];
    [alertView show];
}
- (NSString *)yesButtonTitle{
    return @"Yes";
}
- (NSString *)noButtonTitle{
    return @"No";
}
//@interface ViewController : UIViewController<UIAlertViewDelegate>
//UIAlertViewDelegate 代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqualToString:[self yesButtonTitle]]) {
        NSLog(@"%@",buttonTitle);
    }else if ([buttonTitle isEqualToString:[self noButtonTitle]]){
        NSLog(@"%@",buttonTitle);
    }
    //这里用了 UIAlertView 的 buttonTitleAtIndex 方法。传入一个由零开始的数值索引给这个方法,会得到这个索引所指到的按钮上的本文。可以透过这个方法检测用户按下的按钮。将 alertView:clickedButtonAtIndex 方法传入的 buttonIndex 变量,作为 UIAlertView 中 buttonTitleAtIndex 方法的参数。
}
 
3.使用􏰁示视图要求用户输入文字。 

  例如:当需要要求用户输入信用卡号码或住址时。在此,会用到 UIAlertViewStylePlainTextInput 样式。 

  3.1提示视图可以采用各种样式。UIAlertView 类有􏰁供一个类型为 UIAlertViewStyle 的属 性,叫做 alertViewStyle。 

typedef NS_ENUM(NSInteger, UIAlertViewStyle) {

    UIAlertViewStyleDefault = 0,  //这是默认的样式

    UIAlertViewStyleSecureTextInput, //提示视图会包含一个安全加密的文字栏位。

    UIAlertViewStylePlainTextInput,  //显示一个可见的文字栏位。

    UIAlertViewStyleLoginAndPasswordInput //显示两个本文栏位,一个是可见的用户名称栏位,另一个是加密的密码栏位。

};

  代码:

//设置样式
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput]; 
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    
    //得到alertView上的输入框
    UITextField *textField = [alertView textFieldAtIndex:0];
    NSLog(@"%@",textField.text);
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2014-10-09 12:55  safiri  阅读(270)  评论(0编辑  收藏  举报