如何在iOS中实现弹出式对话框?

314

在进行计算后,我想显示一个弹出框或警示框向用户传达信息。请问有人知道我可以在哪里找到更多相关信息吗?

7个回答

521

没错,你可能正在寻找一个UIAlertView。这是一个例子:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection" 
                                                message:@"You must be connected to the internet to use this app." 
                                               delegate:nil 
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];
[alert release];

如果你想要做一些更花哨的事情,比如在你的UIAlertView中显示自定义UI,你可以对UIAlertView进行子类化,并在init方法中放置自定义UI组件。如果你想要在UIAlertView出现后响应按钮按下事件,你可以设置上方的delegate并实现- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法。

你可能还想查看一下UIActionSheet


45
苹果的文档说明:"UIAlertView类旨在按原样使用,不支持子类化。"。https://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html - JOM
41
仅做评论:启用ARC后,'[alert release]'是不必要的(至少编译器是这样说的)。 - Javier Sedano
5
自 iOS 4 开始,不支持子类化 UIAlertView。 - xySVerma
2
这里是一个简单的带有委托的UIAlertView示例,如果您还需要按钮操作,请参考此处。 - guilherme.minglini
3
如果您正在寻找更快捷的版本,请查看Oscar Swanros的回答。链接为https://dev59.com/dGAg5IYBdhLWcg3wDHbS#24022696。 - Rogerio Chaves
显示剩余3条评论

196
不同的人对于弹出框的理解可能不同。我强烈建议阅读 临时视图 文档。我的回答主要是这份文档和其他相关文档的摘要。

警报框(给我一个例子)

输入图片描述

警报框 显示标题和可选消息。在继续之前,用户必须确认它(一个单按钮警报)或做出简单的选择(一个双按钮警报)。您可以使用 UIAlertController 创建警报。

值得引用文档关于创建不必要的警报的警告和建议。

输入图片描述

注意:

操作表(给我一个例子)

输入图片描述

操作表(Action Sheets)显示给用户一个选项列表,它们会出现在屏幕底部或者弹出视图中,具体取决于设备的尺寸和方向。与警告一样,使用 UIAlertController 来创建操作表。在 iOS 8 之前,使用 UIActionSheet,但是现在文档中指明:

重要提示: UIActionSheet 在 iOS 8 中已被废弃(注意,UIActionSheetDelegate 也已被废弃)。在 iOS 8 或更高版本中,若要创建和管理操作表,请改用带有 preferredStyle 属性设置为 UIAlertControllerStyleActionSheetUIAlertController

模态视图 (显示一个示例)

enter image description here

模态视图是一个自包含的视图,它拥有完成任务所需的一切内容。它可以占据整个屏幕,也可以不是。要创建模态视图,请使用带有一个 模态呈现样式(Modal Presentation Styles)UIPresentationController

另请参见

气泡窗口 (查看示例)

enter image description here

一个气泡窗口是一个视图,当用户点击某个内容时出现并在取消点击后消失。 它有一个箭头,显示控件或位置来自于哪里进行了点击。其内容可以是任何您可以放置在视图控制器中的内容。您可以使用UIPopoverPresentationController创建气泡窗口 (在iOS 8之前,建议使用UIPopoverController方法)。

过去气泡窗口只在iPad上可用,但从iOS 8开始,您也可以在iPhone上获取它们(请参见这里这里这里)。

还请参考

通知

enter image description here

通知是指声音/震动、警报/横幅或标记,即使应用程序不在前台运行也会通知用户某些信息。

在此输入图片描述

参见

关于Android Toast的说明

在此输入图片描述

在Android中,Toast是一条短消息,会在屏幕上显示一段时间,然后自动消失,不会干扰用户与应用程序的交互。

来自Android背景的人想知道iOS版本的Toast是什么。这些问题的一些例子可以在这里这里这里这里找到。答案是:iOS中没有Toast的等效物。已经提出了各种解决方法,包括:

  • 使用自定义的子类化的UIView制作自己的Toast
  • 导入模仿Toast的第三方项目
  • 使用带有定时器的无按钮警报

但是,我的建议是坚持使用已经附带于iOS的标准UI选项。不要尝试使您的应用程序与Android版本完全相同。考虑如何重新打包它,以使其看起来和感觉像iOS应用程序。


3
关于 Android Toast 的说明还在!太好了!这些信息对来自 Android 开发的新开发人员很有帮助。谢谢! - Filipe Brito
然后你有上下文卡片,就像连接AirPods时显示的那个。 - Daniel Nord

62

自从iOS 8发布以来,UIAlertView已被废弃;UIAlertController 是替代品。

这是在Swift 5中的示例:

let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK!", style: .default) { (sender: UIAlertAction) -> Void in
    // ... Maybe handle "OK!" being tapped.
}
alert.addAction(alertAction)

// Show.
present(alert, animated: true) { () -> Void in
    // ... Maybe do something once showing is complete.
}

正如您所见,API允许我们为操作和呈现警报时实现回调函数,这非常方便!

对于旧版Swift:

let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
let alertAction = UIAlertAction(title: "OK!", style: UIAlertActionStyle.default)
{
    (UIAlertAction) -> Void in
}
alert.addAction(alertAction)
present(alert, animated: true)
{
    () -> Void in
}

@Entalpi 应该在 presentViewController 中加上一个闭合括号,而你的 completion block 和仅使用 completion:nil 有什么区别? - Andrew Plummer
没有区别。如果存在一个代码块可以调用,它就会被调用。 - Entalpi

25

自从iOS 8.0以后,您需要使用如下的UIAlertController:

-(void)alertMessage:(NSString*)message
{
    UIAlertController* alert = [UIAlertController
          alertControllerWithTitle:@"Alert"
          message:message
          preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* defaultAction = [UIAlertAction 
          actionWithTitle:@"OK" style:UIAlertActionStyleDefault
         handler:^(UIAlertAction * action) {}];

    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
}
  

在我的例子中,self 是一个 UIViewController,它实现了“presentViewController”方法用于弹出。


12

对于 Swift 3 和 Swift 4:

由于 UIAlertView 已经作废,这里提供了在 Swift 3 上显示警告框的好方法。

let alertController = UIAlertController(title: NSLocalizedString("No network connection",comment:""), message: NSLocalizedString("connected to the internet to use this app.",comment:""), preferredStyle: .alert)
let defaultAction = UIAlertAction(title:     NSLocalizedString("Ok", comment: ""), style: .default, handler: { (pAlert) in
                //Do whatever you want here
        })
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)

已弃用:

这是受检响应启发的 Swift 版本:

显示 AlertView:

   let alert = UIAlertView(title: "No network connection", 
                           message: "You must be connected to the internet to use this app.", delegate: nil, cancelButtonTitle: "Ok")
    alert.delegate = self
    alert.show()

将代理添加到您的视图控制器中:

class AgendaViewController: UIViewController, UIAlertViewDelegate

当用户点击按钮时,将执行此代码:

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {


}

UIAlertView现在已经被弃用。 - Parth Sane

9

虽然我已经写了一个关于不同类型的弹出框的概述,但大多数人只需要一个警告框。

如何实现弹出对话框

enter image description here

class ViewController: UIViewController {

    @IBAction func showAlertButtonTapped(_ sender: UIButton) {

        // create the alert
        let alert = UIAlertController(title: "My Title", message: "This is my message.", preferredStyle: UIAlertController.Style.alert)

        // add an action (button)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))

        // show the alert
        self.present(alert, animated: true, completion: nil)
    }
}

我的详细答案在这里

我更加完整的回答可以在这里找到。

0

这是Xamarin.iOS中的C#版本

var alert = new UIAlertView("Title - Hey!", "Message - Hello iOS!", null, "Ok");
alert.Show();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接