提示框spa
- (void)login{ UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet]; //添加Button [alertController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //处理点击拍照 }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"从相册选取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){ //处理点击从相册选取 }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]]; [self presentViewController: alertController animated: YES completion: nil]; }
警告框code
- (void)zhuce { UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"登录" message: @"输入用户名密码" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"name"; textField.textColor = [UIColor blueColor]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.borderStyle = UITextBorderStyleRoundedRect; }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"password"; textField.textColor = [UIColor blueColor]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.borderStyle = UITextBorderStyleRoundedRect; textField.secureTextEntry = YES; }]; [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSArray * textfields = alertController.textFields; UITextField * namefield = textfields[0]; UITextField * passwordfiled = textfields[1]; NSLog(@"%@:%@",namefield.text,passwordfiled.text); }]]; [self presentViewController:alertController animated:YES completion:nil]; }
第一步 初始化it
+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
这里的preferredStyle有两种,sheet和alertio
typedef enum UIAlertControllerStyle: NSInteger { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } UIAlertControllerStyle;
第二步,添加Action(button或者textfield)
添加Button
- (void)addAction:(UIAlertAction *)action
这里的UIAlertAction是一个比较简单的类class
+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler
style有三种配置
typedef enum UIAlertActionStyle: NSInteger { UIAlertActionStyleDefault = 0,//默认 UIAlertActionStyleCancel,//取消 UIAlertActionStyleDestructive //有可能改变或者数据 } UIAlertActionStyle;
添加TextField
注意,只能是 UIAlertControllerStyleAlert才能添加Textfieldfile
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler
在block里配置textfield的信息,例如placeholder,backgroundcolor等。
Textfield的保存信息可由UIAlertController的属性Textfields得到。如同上述的例子二同样。密码
第三步,显示
例如im
[self presentViewController:alert animated:YES completion:nil];