一。UIAlertControllerStyleActionSheet底部 数组
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) { app
UIAlertControllerStyleActionSheet = 0,底部 spa
UIAlertControllerStyleAlert 中间 code
} server
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"自定义背景" message:nil preferredStyle:UIAlertControllerStyleActionSheet];//建立对象 [self presentViewController:alertController animated:YES completion:nil];//展现alertController,相似于拍照的UIImagePickerController,是一个试图控制器。 UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"cancel");//点击按钮触发的事件在这里写 }];//建立按钮 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"默认default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"默认default"); }]; UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"默认default1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"默认default1"); }]; UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置reset" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSLog(@"reset"); }]; UIAlertAction *resetAction1 = [UIAlertAction actionWithTitle:@"重置reset1" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSLog(@"reset1"); }]; [alertController addAction:cancleAction];//添加按钮 [alertController addAction:defaultAction1]; [alertController addAction:defaultAction]; [alertController addAction:resetAction]; [alertController addAction:resetAction1];
注:在苹果上,Cancel按钮在左边/最底部,其余的按添加顺序显示。 对象
UIAlertActionStyle有三种:UIAlertActionStyleDefault = 0, UIAlertActionStyleCancel,和UIAlertActionStyleDestructive,除了style为cancel只能够addiction一次,其余的能够添加多个按钮。 three
二。添加文本输入框:UIAlertControllerStyleAlert,中间 事件
一、文本输入框只能添加到Alert的风格中,ActionSheet是不容许的; get
二、UIAlertController具备只读属性的textFields数组,须要可直接按本身须要的顺序添加; it
三、添加方式是使用block,参数是UITextField;
四、添加UITextField监听方法和实现方法。
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"自定义背景" message:nil preferredStyle:UIAlertControllerStyleAlert];//建立对象 UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"cancel");//点击按钮触发的事件在这里写 }];//建立按钮 [alertController addAction:cancleAction];//添加上去 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"111111"; }];//添加输入框 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"34444"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldDidChanged:) name:UITextFieldTextDidChangeNotification object:textField];//添加UITextFieldTextDidChangeNotification通知,一旦输入框text改变,在textfieldDidChanged:方法里可检测到输入状况 }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"jianting"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldDidChanged:) name:UITextFieldTextDidChangeNotification object:textField]; }]; UIAlertAction *getAction = [UIAlertAction actionWithTitle:@"默认default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *login = alertController.textFields[0]; UITextField *password = alertController.textFields[1]; UITextField *three = alertController.textFields[2]; [self.view endEditing:YES]; NSLog(@"登录:%@, 密码:%@,three : %@", login.text, password.text,three.text ); }];//获取输入框中的内容 [alertController addAction:getAction];//添加按钮,点击时触发上面handle的操做。 [self presentViewController:alertController animated:YES completion:nil];//展现视图控制器,这个必须写在后面,写在添加textfield以前显示不出输入框的。
- (void)textfieldDidChanged:(NSNotification *)noti {
NSLog(@"\n\n\n%s, noti = %@", __func__, noti);
}
这个是监听方法示例。三。一个自定义标题颜色文字的实现。
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"]; [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50.0] range:NSMakeRange(24, 11)]; [alertVC setValue:hogan forKey:@"attributedTitle"]; UIAlertAction *button = [UIAlertAction actionWithTitle:@"Label text" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ //add code to make something happen once tapped }]; // UIImage *accessoryImage = [UIImage imageNamed:@"someImage"]; // [button setValue:accessoryImage forKey:@"image"]; [alertVC addAction:button]; [self presentViewController:alertVC animated:YES completion:nil];