为了不看到警告,只能用这个了。。。
有了它,就可以通过属性切换警告框和抽屉两种效果了
UIAlertController初始化方法中的preferredStyle参数是一个枚举,决定是提示框或者抽屉列表:
typedef NS_ENUM(NSInteger, UIAlertControllerStyle)
{
UIAlertControllerStyleActionSheet = 0,//抽屉
UIAlertControllerStyleAlert//警告框
}
UIAlertAction的属性和初始化方法
//初始化方法
+ (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;
使用实例
-(void)callAlert
{
UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"AlertController" message:@"不报警高的方法" preferredStyle:UIAlertControllerStyleAlert];
[alertCon addAction:[UIAlertAction actionWithTitle:@"事件点击标题" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action)
{
//点击后触发的事件
}]];
[self presentViewController:alertCon animated:YES completion:nil];
}
AlertAction的风格是如下的枚举:
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,//默认的风格
UIAlertActionStyleCancel,//取消按钮的风格
UIAlertActionStyleDestructive//警告的风格
}
效果如下:
-(void)callAlert2
{
UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"AlertController" message:@"不报警高的方法" preferredStyle:UIAlertControllerStyleAlert];
[alertCon addAction:[UIAlertAction actionWithTitle:@"Destructive" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
{
//点击后触发的事件
}]];
[alertCon addAction:[UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
//点击后触发的事件
}]];
[alertCon addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action)
{
//点击后触发的事件
}]];
[self presentViewController:alertCon animated:YES completion:nil];
}
效果如下:
UIAlertController其他属性和方法
@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;
获取所有AlertAction
@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);
iOS9后新增加的属性,可以使某个按钮更加突出,只能设置已经在actions数组中的AkertAction,会使设置的按钮更加显眼,如下:
-(void)callAlert3
{
UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"AlertController" message:@"不报警高的方法" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"normal" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"freferred" style:UIAlertActionStyleDefault handler:nil];
[alertCon addAction:action];
[alertCon addAction:action2];
alertCon.preferredAction = action2;
[self presentViewController:alertCon animated:YES completion:nil];
}
效果如下:
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
添加一个textField,以前的相关控件,虽然也可以添加textField,但是定制化能力非常差,这个新的方法中有一个configurationHandler代码块,可以将textField的相关设置代码放入这个代码块中,并且这个方法添加的textField个数不再限制于2个:
-(void)callAlert4
{
UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"AlertController" message:@"不报警高的方法" preferredStyle:UIAlertControllerStyleAlert];
[alertCon addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"第一个";
}];
[alertCon addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"第二个";
}];
[alertCon addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"第三个";
}];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"normal" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"freferred" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
//点击后触发的事件
NSArray *arr = [[NSArray alloc]initWithArray:alertCon.textFields];
for (UITextField *field in arr)
{
NSLog(@"%@",field.text);
}
}];
[alertCon addAction:action];
[alertCon addAction:action2];
alertCon.preferredAction = action2;
[self presentViewController:alertCon animated:YES completion:nil];
}
效果如下:
获取所有textField的数组
@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;