【iOS】UI——关于UIAlertController类(警告对话框)

前言

  在UI的警告对话框的学习中,我们发现UIAlertView在iOS 9中已经被废弃,我们找到UIAlertController来代替UIAlertView实现弹出框的功能,从而有了这篇关于UIAlertController的学习笔记。

关于UIAlertController

  UIAlertController 是 iOS SDK 中提供的一个强大且灵活的类,它可以用来显示警告框或者动作表。UIAlertController 取代了早期的 UIAlertView 和 UIActionSheet 类,提供了更加统一和灵活的界面。

具体操作及代码实现

//创建一个UIAlertController对象
//P1:弹出框的标题  P2弹出框的内容  
//P3:弹出的警告框的样式为UIAlertControllerStyleAlert(即中心弹出的警告框)
UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是消息" preferredStyle:UIAlertControllerStyleAlert];

//添加“确认”动作按钮到控制器上
//P1:标题文字  P2:动作样式,有三种动作样式:常规(default)、取消(cancel)以及警示(destruective)
//P3:用户选中并点击该动作时,所执行的代码
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
    // 用户点击确认按钮后执行的代码
}];
//将动作按钮添加到alertController视图上
[alertController addAction:defaultAction];

//添加“取消”动作按钮到控制器上
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
    // 用户点击取消按钮后执行的代码
}];
//将动作按钮添加到alertController视图上
[alertController addAction:cancelAction];

//将警告框显示出来
[self presentViewController:alertController animated:YES completion:nil];

  这个示例创建了一个 UIAlertController,并设置了标题、消息以及样式。然后,我们创建了两个 UIAlertAction,一个是默认的操作,另一个是取消操作(有三种动作样式:常规(default)、取消(cancel)以及警示(destruective))。这两个操作都有处理器,所以当用户点击这些按钮时,会执行相应的代码块。最后,我们使用 presentViewController:animated:completion: 方法来显示警告框。

  将以上代码放进xcode的"ViewController.h"文件的viewDidLoad函数中,运行后你会发现,模拟器屏幕上无任何显示,这是因为:

在 iOS 开发中,警告对话框(UIAlertController)不能直接被呈现,必须在某个存在的视图控制器(UIViewController)上呈现。这是因为UIAlertController实际上是一个视图控制器,而所有的视图控制器都需要一个父视图控制器才能被用户看到。

  为了看到警告框的效果,我们另外创建一个UIButton对象,用来触发警告框。

UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 80, 80);
    btn.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:btn];
    
    [btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];

将UIAlertController相关操作放在btn的press函数里即可。完整代码如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 80, 80);
    btn.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:btn];
    
    [btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
}

- (void) press {
    //创建一个UIAlertController对象
    //P1:弹出框的标题  P2弹出框的内容
    //P3:弹出的警告框的样式为UIAlertControllerStyleAlert(即中心弹出的警告框)
    UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是消息" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:defaultAction];
    
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

@end

按下btn按钮后的运行结果:
在这里插入图片描述

总结

  当我们需要显示一个警告对话框时,我们要在当前的视图控制器上呈现它;如果当前不在视图控制器中,但是需要显示警告对话框,我们需要获取到当前的视图控制器,或者创建一个新的视图控制器来呈现警告对话框。

相关推荐

  1. IOSUI自动化之mobiledevice

    2024-06-08 09:54:05       26 阅读
  2. 关于Flutter doctor里两个警告的消除

    2024-06-08 09:54:05       31 阅读
  3. 如何关闭C源代码中的指定警告

    2024-06-08 09:54:05       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 09:54:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 09:54:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 09:54:05       82 阅读
  4. Python语言-面向对象

    2024-06-08 09:54:05       91 阅读

热门阅读

  1. SQL入门教程

    2024-06-08 09:54:05       24 阅读
  2. Unit9

    2024-06-08 09:54:05       34 阅读
  3. adb批量安装

    2024-06-08 09:54:05       29 阅读
  4. 简说SQLServer

    2024-06-08 09:54:05       33 阅读
  5. @Scheduled注解创建定时任务的 3 种模式

    2024-06-08 09:54:05       30 阅读
  6. 2020年06月C语言二级真题

    2024-06-08 09:54:05       31 阅读
  7. npm run *** 上传 dist 到 github

    2024-06-08 09:54:05       30 阅读
  8. 基于vue3实现倒计时60s的

    2024-06-08 09:54:05       29 阅读
  9. PostgreSQL的视图pg_stat_user_indexes

    2024-06-08 09:54:05       30 阅读