Skip to content

UIAlertController ​

UIAlertController 表示一个系统弹窗:一段标题、一段说明文字,加上若干个按钮。弹窗盖在当前界面上面,用户点了其中一个按钮之后它才消失。弹窗有两种样式,创建时选定:屏幕中间的提示框,和从屏幕底部升起来的菜单。

弹窗上的每个按钮是一个 UIAlertAction。用户点了哪个按钮,哪个按钮派发 click 事件;弹窗自己没有事件。

弹窗是一个页面。它没有自己的「显示」方法,用页面的 presentViewController() 弹出来。弹窗的外观由系统决定,App 只提供文字和按钮。

注意:创建、属性的读写和报错在模拟器(iPhone Air,iOS 27.2)的验收里验过。屏幕上的样子、点按钮之后发生的事,要有人真的点一下才验得到,都未在模拟器验证。

继承关系:EventTarget → NSObject → UIResponder → UIViewController → UIAlertController

怎么做提示框、底部菜单和带输入框的弹窗,见指南弹出提示框和菜单。

构造函数 ​

不能用 new 创建。new UIAlertController() 抛 TypeError: Illegal constructor: UIAlertController has no public initializer; use UIAlertController.alertControllerWithTitle(title, { message, preferredStyle })。用下面的静态方法创建。

静态方法 ​

UIAlertController.alertControllerWithTitle()

创建一个弹窗,给出标题、说明文字和样式。

实例属性 ​

还继承 UIViewController 的属性。弹窗的标题就是继承来的 title。

属性说明
message标题下面的说明文字
preferredStyle 只读样式:提示框或底部菜单
severity严重程度
actions 只读加进来的全部按钮
preferredAction提示框里加粗显示的那个按钮
textFields 只读加进来的全部输入框

实例方法 ​

还继承 UIViewController 的方法。用代码关掉弹窗用继承来的 dismissViewController()。

方法说明
addAction()加一个按钮
addTextField()加一个输入框,只有提示框能加

事件 ​

弹窗没有自己的事件。用户点了哪个按钮,在那个按钮上监听 click。

事件什么时候派发
traitchange继承自 UIViewController:外观特征变了。在弹窗上未在模拟器验证

页面的关闭事件 beforedismiss、dismiss、dismissattempt 在弹窗上不会派发,两种样式都是:系统不让弹窗报告自己被关掉。监听它们不会报错,它们被当成自定义事件,只有自己用 dispatchEvent() 派发时才会触发。

两种样式 ​

preferredStyle出现在哪能带输入框吗适合
'alert'屏幕中间能告知一件事;让用户确认;让用户输入一小段文字
'actionSheet'从屏幕底部升起来不能,调 addTextField() 抛 TypeError在几个操作里选一个

样式在创建时给,之后不能改。

弹窗什么时候关 ​

用户点弹窗上的任何一个按钮,系统都会把弹窗关掉。App 不能让某个按钮「点了不关」。

  • 被点的那个按钮在弹窗关掉之后派发一次 click,其余的按钮什么都不派发。未在模拟器验证。
  • 没有监听器的按钮也能点,点了只是把弹窗关掉。
  • 禁用的按钮点不动:点它弹窗不关,也不派发事件。未在模拟器验证。
  • 用代码 dismissViewController() 关掉弹窗不算点了按钮,没有按钮派发事件。未在模拟器验证。
  • 底部菜单可以点菜单外面关掉。这等于点了取消样式的那个按钮,那个按钮派发事件。未在模拟器验证。
  • 提示框点外面关不掉。一个按钮都没有的提示框,用户没有办法关掉它,只能由代码关。未在模拟器验证。
  • 弹窗关掉之后,textFields 里的输入框还在,text 是用户输入的内容。

示例 ​

问用户要不要删除 ​

从空白 App 开始,在页面上放一个按钮。点它弹出一个提示框,问用户要不要删除。

js
application.addEventListener('sceneconnect', (e) => {
  const win = new UIWindow(e.scene);
  const page = new UIViewController();
  page.view.backgroundColor = 'systemBackground';
  win.rootViewController = page;
  win.makeKeyAndVisible();

  const button = UIButton.buttonWithType('system');
  button.setTitle('删除照片', { forState: ['normal'] });
  button.frame = new CGRect(40, 120, 200, 44);
  page.view.addSubview(button);

  button.addEventListener('click', () => {
    const alert = UIAlertController.alertControllerWithTitle('删除这张照片?', {
      message: '删除之后不能恢复。',
      preferredStyle: 'alert',
    });
    const cancel = UIAlertAction.actionWithTitle('取消', { style: 'cancel' });
    const remove = UIAlertAction.actionWithTitle('删除', { style: 'destructive' });
    remove.addEventListener('click', () => {
      console.log('照片删掉了');
    });
    alert.addAction(cancel);
    alert.addAction(remove);
    page.presentViewController(alert, { animated: true });
  });
});

效果(未在模拟器验证)

  • 页面左上方有一个蓝色文字的按钮「删除照片」。
  • 点它,屏幕中间出现一个提示框,标题是「删除这张照片?」,下面一行说明,底下两个按钮「取消」和红色的「删除」。
  • 点「删除」,提示框消失,控制台输出 照片删掉了。
  • 点「取消」,提示框消失,控制台没有输出。

更多写法见指南弹出提示框和菜单。

对应的 iOS API ​

ObjCJS
[UIAlertController alertControllerWithTitle:@"标题" message:@"说明" preferredStyle:UIAlertControllerStyleAlert]UIAlertController.alertControllerWithTitle('标题', { message: '说明', preferredStyle: 'alert' })
[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction *a) { … }]const a = UIAlertAction.actionWithTitle('好', { style: 'default' });
a.addEventListener('click', () => { … })
[alert addAction:action]alert.addAction(action)
[alert addTextFieldWithConfigurationHandler:^(UITextField *f) { f.placeholder = @"名字"; }]const f = alert.addTextField();
f.placeholder = '名字'
alert.textFields.firstObject.textalert.textFields[0].text
alert.preferredAction = actionalert.preferredAction = action
action.enabled = NOaction.isEnabled = false
[self presentViewController:alert animated:YES completion:nil]page.presentViewController(alert, { animated: true })

参见 ​