UIAlertController的正常用法
// 上传
- (void)btnClick{
/* preferredStyle有且只有这两种枚举类型
* UIAlertControllerStyleActionSheet 在屏幕底部弹出
* UIAlertControllerStyleAlert 在屏幕中间弹出
*/
// UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"标题" message:@"副标题" preferredStyle:UIAlertControllerStyleAlert];
UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"标题" message:@"副标题" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *Action1 = [UIAlertAction actionWithTitle:@"普通按钮" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
//普通按钮
NSLog(@"普通按钮是蓝色的");
}];
UIAlertAction *Action2 = [UIAlertAction actionWithTitle:@"警告按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *_Nonnull action) {
//普通按钮
NSLog(@"带有警告意味的按钮是红色的");
}];
UIAlertAction *Action3 = [UIAlertAction actionWithTitle:@"取消按钮" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
//普通按钮
NSLog(@"取消按钮也是蓝色的,并且始终在最下面");
}];
// //如果是UIAlertControllerStyleActionSheet不能使用添加输入框的方法
// [alertControl addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
// //添加输入框(已经自动add,不需要手动)
//
// textField.text = @"可以在这里写textfield的一些属性";
//
// //监听
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listening:) name:UITextFieldTextDidChangeNotification object:textField];
// }];
[alertControl addAction:Action1];
[alertControl addAction:Action2];
[alertControl addAction:Action3];
[self presentViewController:alertControl animated:YES completion:nil];
}
监听输入框的输入
//- (void)listening:(NSNotification *)noti{
// NSLog(@"%@", noti.object);
//}
UIAlertControllerStyleAlert 在屏幕中间弹出UIAlertControllerStyleActionSheet 在屏幕底部弹出
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
UIAlertController的自定义(利用富文本与kvc)
UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"标题" message:@"副标题" preferredStyle:UIAlertControllerStyleAlert];
// 自定义标题
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"这是标题"];
[title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0,4)];
[title addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,4)];
[alertControl setValue:title forKey:@"attributedTitle"];
// 自定义副标题
NSMutableAttributedString *subTitle = [[NSMutableAttributedString alloc] initWithString:@"这是副标题"];
[subTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0,5)];
[subTitle addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0,5)];
[alertControl setValue:subTitle forKey:@"attributedMessage"];
// 自定义取消按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 设置按钮背景图片
UIImage *image = [[UIImage imageNamed:@"icon_182"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[cancelAction setValue:image forKey:@"image"];
// 设置按钮的title颜色
[cancelAction setValue:[UIColor orangeColor] forKey:@"titleTextColor"];
// 设置按钮的title的对齐方式
[cancelAction setValue:[NSNumber numberWithInteger:NSTextAlignmentLeft] forKey:@"titleTextAlignment"];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
[alertControl addAction:okAction];
[alertControl addAction:cancelAction];
[self presentViewController:alertControl animated:YES completion:nil];
效果图: