警示框UIAlertController的使用(看完马上会用!!)
本文尽量图文并茂,并且提供对应的代码,确保看到这篇文章马上能够上手使用UIAlertController控件。~我要兑现我的务实宣言~
本文构思:
1、出具效果图,通过这种最直接方式了解该控件的展示效果看看是不是所需要的。
2、每种效果图对应的代码,绝对是拿出去直接可以显示出效果的。
3、根据苹果官方给出的UIAlertController的声明文件,总的再进行一次梳理知识。
为了描述,下面使用的是图1、图2、图3等等,来指代上面展示的7张图。接下来给出每个图对应的代码。
// 图一 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定取消活动?" preferredStyle:UIAlertControllerStyleAlert]; // UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; // UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型 // UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置 // 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { }]; // 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开 [alert addAction:sureAction]; [alert addAction:cancelAction]; // 只能使用模态的方式切入 [self.viewController presentViewController:alert animated:NO completion:nil];
// 图二 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定取消活动?" preferredStyle:UIAlertControllerStyleAlert]; // UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; // UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型 // UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置 // 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { }]; // UIAlertActionStyleDestructive颜色为红色,谨慎操作效果 UIAlertAction *maybeAction1 = [UIAlertAction actionWithTitle:@"随机1" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { }]; // UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *maybeAction2 = [UIAlertAction actionWithTitle:@"随机2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; // UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *maybeAction3 = [UIAlertAction actionWithTitle:@"随机3" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; // enabled = NO;按钮颜色变为灰色,并且不可点击 maybeAction3.enabled = NO; // 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开 [alert addAction:sureAction]; [alert addAction:cancelAction]; [alert addAction:maybeAction1]; [alert addAction:maybeAction2]; [alert addAction:maybeAction3]; // 只能使用模态的方式切入 [self.viewController presentViewController:alert animated:NO completion:nil];
// 图三 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定取消活动?" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型 // UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置 // 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDestructive颜色为红色,谨慎操作效果 UIAlertAction *maybeAction1 = [UIAlertAction actionWithTitle:@"随机1" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *maybeAction2 = [UIAlertAction actionWithTitle:@"随机2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *maybeAction3 = [UIAlertAction actionWithTitle:@"随机3" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// enabled = NO;按钮颜色变为灰色,并且不可点击 maybeAction3.enabled = NO;
// 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开 [alert addAction:sureAction]; [alert addAction:cancelAction]; [alert addAction:maybeAction1]; [alert addAction:maybeAction2]; [alert addAction:maybeAction3];
// 默认是UIAlertActionStyleCancel类型的UIAlertAction按钮 // preferredAction后的UIAlertAction按钮,字体加粗,颜色等不变。只在UIAlertControllerStyleAlert时有效 // 但是依然是UIAlertActionStyleCancel类型的UIAlertAction按钮在最右边(最下面)的位置 alert.preferredAction = maybeAction2; // 只能使用模态的方式切入 [self.viewController presentViewController:alert animated:NO completion:nil];
// 图四 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定取消活动?" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型 // UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置 // 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { }]; // 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开 [alert addAction:sureAction]; [alert addAction:cancelAction]; // reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'==TextField只能在Alert是UIAlertControllerStyleAlert类型的时候才可以使用。 // 添加过多的TextField,Alert势必会显示不下。系统采取的策略是,先让Textfield区域往下延伸,使得UIAlertAction区域进行滚动。当把UIAlertAction区域拥挤到只能显示两个按钮,开始让Textfield区域也可以滚动。 [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { }]; // 可以通过textFields方法,取出添加进去的TextField,然后对UITextField对象进行操作 NSArray<UITextField *> *alertTextField = [alert textFields]; UITextField *firstTextField = [alertTextField firstObject]; firstTextField.text = @"firstTextField"; // 只能使用模态的方式切入 [self.viewController presentViewController:alert animated:NO completion:nil];
// 图五 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定取消活动?" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型 // UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置 // 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDestructive颜色为红色,谨慎操作效果 UIAlertAction *maybeAction1 = [UIAlertAction actionWithTitle:@"随机1" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *maybeAction2 = [UIAlertAction actionWithTitle:@"随机2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *maybeAction3 = [UIAlertAction actionWithTitle:@"随机3" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// enabled = NO;按钮颜色变为灰色,并且不可点击 maybeAction3.enabled = NO;
// 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开 [alert addAction:sureAction]; [alert addAction:cancelAction]; [alert addAction:maybeAction1]; [alert addAction:maybeAction2]; [alert addAction:maybeAction3];
// 默认是UIAlertActionStyleCancel类型的UIAlertAction按钮 // preferredAction后的UIAlertAction按钮,字体加粗,颜色等不变。只在UIAlertControllerStyleAlert时有效 // 但是依然是UIAlertActionStyleCancel类型的UIAlertAction按钮在最右边(最下面)的位置 alert.preferredAction = maybeAction2; // reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'==TextField只能在Alert是UIAlertControllerStyleAlert类型的时候才可以使用。 // 添加过多的TextField,Alert势必会显示不下。系统采取的策略是,先让Textfield区域往下延伸,使得UIAlertAction区域进行滚动。当把UIAlertAction区域拥挤到只能显示两个按钮,开始让Textfield区域也可以滚动。 [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { }]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
}];
// 可以通过textFields方法,取出添加进去的TextField,然后对UITextField对象进行操作 NSArray<UITextField *> *alertTextField = [alert textFields]; UITextField *firstTextField = [alertTextField firstObject]; firstTextField.text = @"firstTextField"; // 只能使用模态的方式切入 [self.viewController presentViewController:alert animated:NO completion:nil];
// 图六 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定取消活动?" preferredStyle:UIAlertControllerStyleActionSheet];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型 // UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置 // 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { }]; // 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开 [alert addAction:sureAction]; [alert addAction:cancelAction]; // 只能使用模态的方式切入 [self.viewController presentViewController:alert animated:NO completion:nil];
// 图七 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定取消活动?" preferredStyle:UIAlertControllerStyleActionSheet];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleCancel,只能有一个UIAlertAction实例对象为这种类型 // UIAlertActionStyleCancel类型的UIAlertAction按钮,永远都在最右边(最下面)的位置 // 默认UIAlertActionStyleCancel类型的UIAlertAction按钮,为preferredAction UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDestructive颜色为红色,谨慎操作效果 UIAlertAction *maybeAction1 = [UIAlertAction actionWithTitle:@"随机1" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { }];
// UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *maybeAction2 = [UIAlertAction actionWithTitle:@"随机2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; // UIAlertActionStyleDefault,可以多个按钮为这个类型 UIAlertAction *maybeAction3 = [UIAlertAction actionWithTitle:@"随机3" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
// enabled = NO;按钮颜色变为灰色,并且不可点击 maybeAction3.enabled = NO;
// 添加3个以上的UIAlertAction按钮,就竖向排开。2个(含)以内就是水平排开 [alert addAction:sureAction]; [alert addAction:cancelAction]; [alert addAction:maybeAction1]; [alert addAction:maybeAction2]; [alert addAction:maybeAction3];
// 默认是UIAlertActionStyleCancel类型的UIAlertAction按钮 // preferredAction后的UIAlertAction按钮,字体加粗,颜色等不变。只在UIAlertControllerStyleAlert时有效 // 但是依然是UIAlertActionStyleCancel类型的UIAlertAction按钮在最右边(最下面)的位置 alert.preferredAction = maybeAction2; // 只能使用模态的方式切入 [self.viewController presentViewController:alert animated:NO completion:nil];
梳理知识方面的话,我觉得如果对着上面的效果图,再加上把代码运行在Xcode上的话,关于这个控件的时候我相信应该是已经掌握了的。
~本文到此结束,如果后面苹果SDK的变动影响到这个知识了,我会及时更新的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了