UIAlertView笔记

时间:2022-03-21 14:33:22

链接地址:http://www.cnblogs.com/scandy-yuan/archive/2013/03/11/2954194.html

1. 最简单的用法

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

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

                                                  delegate:nil   

                                                  cancelButtonTitle:@"确定" 

                                                  otherButtonTitles:nil];  

[alert show];  
UIAlertView笔记

UIAlertView笔记

2. 为UIAlertView添加多个按钮

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

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

                                                  delegate:nil   

                                                  cancelButtonTitle:@"取消" 

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

UIAlertView笔记

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

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

UIAlertView笔记
头文件:

@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];

}
UIAlertView笔记

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

UIAlertView笔记

4.修改提示框样式

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

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

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

UIAlertView笔记

UIAlertViewStyleSecureTextInput显示为:

UIAlertView笔记

UIAlertViewStyleLoginAndPasswordInput为:

UIAlertView笔记

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

UIAlertView笔记
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];
UIAlertView笔记

UIAlertView笔记    UIAlertView笔记

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

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