一、UIAlertController
1. 概述
2. 属性
+ (instancetype)alertControllerWithTitle:(nullableNSString *)title message:(nullableNSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0, // 上拉菜单样式
UIAlertControllerStyleAlert // 对话框样式,默认样式
} NS_ENUM_AVAILABLE_IOS(8_0);
@property (nullable,nonatomic, copy)NSString *title;
3)设置 UIAlertController 的消息
@property (nullable,nonatomic, copy)NSString *message;
4)保存 UIAlertController 的风格样式,只能在初始化时设置
@property (nonatomic,readonly) UIAlertControllerStyle preferredStyle;
5)向 UIAlertController 对象添加动作按钮
- (void)addAction:(UIAlertAction *)action;
6)保存 UIAlertController 对象的所有动作按钮的数组
@property (nonatomic,readonly) NSArray<UIAlertAction *> *actions;
7)向 UIAlertController 对象添加文本框对象,并设置文本框的代码块
- (void)addTextFieldWithConfigurationHandler:(void (^__nullable)(UITextField *textField))configurationHandler;
8)保存 UIAlertController 对象的所有文本框的数组
@property (nullable,nonatomic, readonly)NSArray<UITextField *> *textFields;
9)优先选择的按钮;将该属性设置为某个动作按钮时,该按钮会加粗显示
@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);
当在一个视图控制器中系那是另一个视图控制器时,可以使用方法
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);
此方法只在没有 UINavigationController 时使用
二、UIAlertAction
1. 概述
UIAlertAction 用于向 UIAlertController 对象中添加动作按钮
2. 属性
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0, // 默认风格
UIAlertActionStyleCancel, // 取消风格
UIAlertActionStyleDestructive // 警示风格,以红色字显示
} NS_ENUM_AVAILABLE_IOS(8_0);
@property (nullable, nonatomic, readonly) NSString *title;
3)获取动作按钮的风格
@property (nonatomic, readonly) UIAlertActionStyle style;
4)设置动作按钮是否激活
@property (nonatomic, getter=isEnabled) BOOL enabled;