NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler; //创建操作
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle; //初始化
- (void)addAction:(UIAlertAction *)action; //添加操作
- 示例1:最简单的提醒视图
UIAlertAction *okAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//具体操作内容
}]; //创建操作
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"顶部标题栏"message:@"中间信息"preferredStyle:UIAlertControllerStyleAlert]; //初始化
[alert addAction:okAlert]; //添加操作
[self presentViewController:alert animated:YES completion:nil]; //以model形式,显示警告视图
NSString *title = @"顶部标题栏";
NSString *message = @"中间信息";
NSString *okAlertButton = @"OK";
UIAlertAction *okAlert = [UIAlertAction
actionWithTitle:okAlertButton style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {
}]; // 也类似于这样的
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
- 示例2:多个按键的提醒视图
[alert addAction:[UIAlertAction actionWithTitle:@"下次下次" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击取消按钮");
}]]; //取消按键——下次下次 UIAlertActionStyleCancel (粗体)
[alert addAction:[UIAlertAction actionWithTitle:@"残忍拒绝" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"红色按钮");
}]]; //红色按键——残忍拒绝 UIAlertActionStyleDestructive
// 这是两个的样子
[alert addAction:[UIAlertAction actionWithTitle:@"立马好评" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"普通按钮");
}]]; // 3个就挨个往下排 //普通按键——立马好评 UIAlertActionStyleDefault
- 示例3:带对话框TextField
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
NSLog(@"TextField");
//中间的提示输入
textField.placeholder = @"用来提示你做什么事的";
}]; //添加TextField 条栏 addTextFieldWithConfigurationHandler
- 示例4:提醒图标,在最底部呈现 Sheet
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet]; // UIAlertControllerStyleAlert改成UIAlertControllerStyleActionSheet (最后面的) 其余的不用变