提示框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];