Sam大叔
"if you ever want something badly,let it go.if it comes back to you,then it's yours forever.if it doesn't,then it was never yours to begin with."

导航

 

1. 最简单的用法

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 

                                                  message:@"这是一个简单的警告框!" 

                                                  delegate:nil   

                                                  cancelButtonTitle:@"确定" 

                                                  otherButtonTitles:nil];  

[alert show];  

 

 

2. 为UIAlertView添加多个按钮

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 

                                                  message:@"请选择一个按钮:" 

                                                  delegate:nil   

                                                  cancelButtonTitle:@"取消" 

                                                  otherButtonTitles:@"按钮一", 
                                                                          @"按钮二",
                                                                          @"按钮三", nil]
[alert show];                      

 

 

 3. 如何判断用户点击的按钮

UIAlertView有一个委托UIAlertViewDelegate ,继承该委托来实现点击事件

头文件:

@interface MyAlertViewViewController : UIViewController<UIAlertViewDelegate> {

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

-(IBAction) buttonPressed;

@end


源文件:

-(IBAction) buttonPressed

{

UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" 

                                                 message:@"请选择一个按钮:" 

                                                 delegate:self   

                                                 cancelButtonTitle:@"取消" 

                                                 otherButtonTitles:@"按钮一", @"按钮二", @"按钮三",nil];  

[alert show];  

}

 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d个按钮!",buttonIndex];

 

UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示"

                                                 message:msg

                                                 delegate:nil

                                                 cancelButtonTitle:@"确定"

                                                 otherButtonTitles:nil];

 

[alert show];

}

 

点击“取消”,“按钮一”,“按钮二”,“按钮三”的索引buttonIndex分别是0,1,2,3

 

4.修改提示框样式

iOS5中UIAlertView新增了一个属性alertViewStyle,它的类型是UIAlertViewStyle,是一个枚举值:

typedef enum {
    UIAlertViewStyleDefault = 0,
    UIAlertViewStyleSecureTextInput,
    UIAlertViewStylePlainTextInput,
    UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle;

alertViewStyle属性默认是UIAlertViewStyleDefault。我们可以把它设置为UIAlertViewStylePlainTextInput,那么AlertView就显示为这样:

UIAlertViewStyleSecureTextInput显示为:

UIAlertViewStyleLoginAndPasswordInput为:

 

当然我们也可以通过创建UITextField来关联这里的输入框并设置键盘响应的样式

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"CD-KEY" 

                                                 message:@"please enter cd-key:" 

                                                 delegate:self   

                                                 cancelButtonTitle:@"Cancel" 

                                                 otherButtonTitles:@"Ok",nil];  
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

UITextField * text1 = [alert textFieldAtIndex:0];

UITextField * text2 = [alert textFieldAtIndex:1];

text1.keyboardType = UIKeyboardTypeNumberPad;

text2.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

[alert show];

 

    

UIAlertViewStyleSecureTextInput和UIAlertViewStylePlainTextInput可以通过textFieldIndex为0来获取输入框对象。

UIAlertViewStyleLoginAndPasswordInput可以通过textFieldIndex为0和1分别获取用户名输入框对象和密码输入框对象。

 

posted on 2013-03-11 16:15  Sam大叔  阅读(14613)  评论(0编辑  收藏  举报