一 UIAlertController的学习
UIAlertController的API很简单,其官网API戳这里。关于UIAlertController的API也非常简单,所有内容如下图所示。从图中我们可以看到UIAlertController的内容主要分为五个部分:创建对象、配置UIAlertController对象的属性、配置UIAlertController上面的按钮、配置UIAlertController上面的文本框、常量。下面,我们结合实例对这些方法和常量进行学习。

UIAlertController提示器的使用分为三步,创建UIAlertController提示器对象-->配置UIAlertController提示器上的按钮-->显示UIAlertController提示器。
1.1 UIAlertController提示器对象的创建
UIAlertController提示器的创建主要是通过类方法来进行创建的,其中第一个参数是标题,第二个参数是内容信息,第三个参数UIAlertControllerStyle则是选择所创建的UIAlertController对象的类型是UIAlertView 还是 UIActionSheet。UIAlertControllerStyle是一个枚举类型,其定义就是在UIAlertController.h文件中。
+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
//UIAlertView风格
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
//UIActionSheet风格
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleActionSheet];
1.2 UIAlertController提示器的配置
在UIAlertController提示器中,我们常用的配置有两类,一是根据需要添加按钮,并味蕾个按钮添加点击事件;二是根据需要添加文本框,用于和用户进行更多的交互。
1.2.1 UIAlertController上添加按钮
UIAlertController上的每一个按钮都是一个UIAlertAction,与UIAlertController的类型是UIAlertView 还是 UIActionSheet无关。UIAlertAction的定义也是就在UIAlertController.h文件中,如下。我们需要在UIAlertController提示器添加一个按钮时,先创建一个UIAlertAction,然后通过UIAlertController的 addAction: 方法将创建的UIAlertAction对象添加就OK了。
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject <NSCopying>
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;
@property (nullable, nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) UIAlertActionStyle style;
@property (nonatomic, getter=isEnabled) BOOL enabled;
@end
创建UIAlertAction对象直接用UIAlertAction的类方法就可以创建了,其中第一个参数是按钮的标题;第二个参数UIAlertActionStyle是选择按钮的风格类型,有三种选择:常规、取消和销毁风格类型;第三个参数是一个Block,定义了按钮的点击响应事件。
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;
UIAlertActionStyle是一个枚举类型,其定义也是在UIAlertController.h文件中。
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0, //常规类型,默认蓝色字体
UIAlertActionStyleCancel, //取消类型,默认蓝色字体
UIAlertActionStyleDestructive //销毁类型,默认红色字体,表示可能是要删除信息
} NS_ENUM_AVAILABLE_IOS(8_0);
//创建对象
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//添加销毁按钮
UIAlertAction* destructiveBtn = [UIAlertAction actionWithTitle:@"销毁按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
NSLog(@"UIAlertActionStyleDestructive");
}];
[alert addAction: destructiveBtn];
//添加默认按钮
UIAlertAction* defaultBtn = [UIAlertAction actionWithTitle:@"常规按钮" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"UIAlertActionStyleDefault");
}];
[alert addAction:albumBtn];
//添加取消按钮
UIAlertAction* cancelBtn = [UIAlertAction actionWithTitle:@"取消按钮" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSLog(@"UIAlertActionStyleCancel");
}];
[alert addAction:cancelBtn];
1.2.2 UIAlertController上添加文本框
上面我们讲到了如何在UIAlertController提示器上添加按钮,但是有时候,我们需要在提示器上添加一个或多个文本框让用户填写一些信息,在UIAlertController中也提供了一个方法直接可以在提示器上添加文本框。只有一个参数,就是一个Block,用于我们队该文本框进行配置,比喻说其字体大小,行数限制等等,都可以在该Block中进行设置。
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
NSLog(@"添加一个textField就会调用 这个block");
}];
值得注意的有两点:
文本框的添加只能是在UIAlertController的风格类型为UIAlertView时才有
文本框的添加多个
我们可以看到,在配置文本框这里还有一个参数是textFields,这各参数是一个只读数组类型,用于获取UIAlertController提示器上所有的文本框对象,这个经常在我们点击按钮时用这个来获取到每一个文本框,并取得用户填写的信息。
@property(nonatomic, readonly) NSArray<UITextField *> *textFields;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
//添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//设置键盘输入为数字键盘
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.placeholder = @"请填写";
}];
UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//取消
}];
[alert addAction: cancelBtn];
//添加确定按钮
UIAlertAction *confirmBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//文本框结束编辑,收起键盘
[[alertVC.textFields firstObject] endEditing:YES];
NSLog(@"%@", [alert.textFields firstObject].text);
//获取文本框填写的内容
NSString *meetingId = [alertVC.textFields firstObject].text.trim;
if(meetingId.length > 12){
[weakSelf showHUDWithText:@"会议号过长"];
}else{
[weakSelf enterVideoMeeting:meetingId];
}
}];
[alert addAction: confirmBtn];
1.3 UIAlertController提示器的显示
UIAlertController提示器的显示则很简单,从提示器的类名UIAlertController可以看出,提示器是一个viewController,因此,要显示提示器,我们一般是是当前viewController的 presentViewController: animated: completion: 方法进行推出,我们创建的提示器。
[self presentViewController:alert animated:YES completion:nil];
1.4 UIAlertController提示器的使用
//创建对象
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"显示的标题" message:@"标题的提示信息" preferredStyle:UIAlertControllerStyleAlert];
//添加取消类型按钮
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击取消");
}]];
//添加常规类型按钮
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击确认");
}]];
//添加销毁类型按钮
[alertController addAction:[UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击警告");
}]];
//添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
NSLog(@"添加一个textField就会调用 这个block");
}];
//显示
[self presentViewController:alertController animated:YES completion:nil];
运行的效果图如下图所示,左边是UIAlertView类型的效果图,右边是UIActionSheet类型的效果图。


二 UIAlertController中自定义
在一般情况下,我们只要弹出系统自带的弹出框就可以。but,在某些情况下,万恶的UI会要求你修改显示文字的大小、颜色,虽然系统自带有一种红色字体的UIAlertAction,但是这种Action并不能放在Cancel位置,所以,更多时候,需要我们自己修改文字字体和颜色。可是在公开的API接口中好像并没有对应的方法,那么我们应该怎么做呢?主要的方法有两种:
- 利用第三方控件
- 利用KVC方法进行自定义修改
2.1 利用第三方控件进行UIAlertController属性的自定义
现在Github上有着众多的Alert控件(如SCLAlertView等),相信有很多都可以满足大家的需求,只要使用Cocoapods添加添加第三方库就可以了。在这里我们就不详细进行介绍了。
2.2 利用KVC方法进行UIAlertController属性的自定义
有时候使用第三方控件会带来很多不必要的代码量和bug,所以能用系统自带的UIAlertController解决是最好的办法,这样当然也是可以的。苹果公司并没有完全的封死对UIAlertController的定制,而是修改为使用KVC的方法进行定制。如果要自定义标题和内容,可以通过NSAttributedString把字体和颜色设置好,然后在通过KVC的方法进行设置,就可以了。
- (void) test{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleActionSheet];
//修改title
NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];
[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
[alertController setValue:alertControllerStr forKey:@"attributedTitle"];
//修改message
NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];
[alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];
[alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
//常规按钮
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil];
//销毁按钮
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"Destructive" style:UIAlertActionStyleDestructive handler:nil];
//取消按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:defaultAction];
[alertController addAction:destructiveAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
效果如下图所示:

除了可以修改提示器的标题和内容信息的颜色和字号,我们还可以修改按钮控件的颜色和字号,具体方法如下:
//修改按钮
if (cancelAction valueForKey:@"titleTextColor") {
[cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
}
效果图如下图所示:

网友评论