【问题标题】:How to implement a pop-up dialog box in iOS?iOS中如何实现弹出对话框?
【发布时间】:2011-06-26 16:34:44
【问题描述】:

计算后,我想显示一个弹出框或警告框,向用户传达消息。有谁知道我在哪里可以找到有关此的更多信息?

【问题讨论】:

    标签: ios popup


    【解决方案1】:

    是的,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

    【讨论】:

    • Apple 文档说“UIAlertView 类旨在按原样使用,不支持子类化”。 developer.apple.com/library/ios/#documentation/uikit/reference/…
    • 只是注释:启用 ARC 后,不需要“[警报发布]”(至少,编译器是这样说的)。
    • 从 iOS 4 开始不支持子类化 UIAlertView
    • Here 是一个带有委托的简单 UIAlertView 示例,如果您还需要按钮操作
    • 如果您正在寻找 swift 版本,请查看Oscar Swanros' answer
    【解决方案2】:

    自 iOS 8 发布以来,UIAlertView 现已弃用; UIAlertController 是替代品。

    这是它在 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
    }
    

    如您所见,API 允许我们为操作和显示警报实现回调,这非常方便!

    为 Swift 4.2 更新

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

    【讨论】:

    • @Entalpi presentViewController 应该有一个右大括号,你的完成块和只有 completion:nil 有什么区别?
    • 没有区别。如果存在一个块来调用它就会被调用。
    【解决方案3】:

    提出这个问题的不同人通过弹出框表示不同的意思。我强烈推荐阅读Temporary Views 文档。我的回答主要是对本文档和其他相关文档的总结。

    提醒(show me an example)

    Alerts 显示标题和可选消息。用户必须在继续之前确认它(一键警报)或做出简单的选择(两键警报)。您使用UIAlertController 创建警报。

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

    注意事项:

    行动表(show me an example)

    Action Sheets 给用户一个选择列表。它们出现在屏幕底部或弹出窗口中,具体取决于设备的大小和方向。与警报一样,UIAlertController 用于制作操作表。在 iOS 8 之前,使用 UIActionSheet,但现在 documentation 表示:

    重要提示: UIActionSheet 在 iOS 8 中已弃用。(请注意,UIActionSheetDelegate 也已弃用。)要在 iOS 8 及更高版本中创建和管理操作表,请改用 UIAlertControllerpreferredStyleUIAlertControllerStyleActionSheet

    模态视图(show me an example)

    modal view 是一个独立的视图,包含完成任务所需的一切。它可能会也可能不会占据全屏。要创建模态视图,请使用 UIPresentationControllerModal Presentation Styles 之一。

    另见

    弹窗(show me an example)

    Popover 是一个视图,当用户点击某物时出现,点击它时消失。它有一个箭头,显示进行点击的控件或位置。内容可以是您可以放入视图控制器中的任何内容。您使用UIPopoverPresentationController 制作弹出框。 (在 iOS 8 之前,UIPopoverController 是推荐的方法。)

    过去弹出框只能在 iPad 上使用,但从 iOS 8 开始,您也可以在 iPhone 上使用它们(请参阅hereherehere)。

    另见

    通知

    Notifications 是声音/振动、警报/横幅或徽章,即使应用程序未在前台运行,也会通知用户某事。

    另见

    关于 Android Toasts 的说明

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

    具有 Android 背景的人想知道 iOS 版本的 Toast 是什么。他可以找到这些问题的一些示例herehereherehere。答案是在 iOS 中没有与 Toast 等效的功能。已提出的各种解决方法包括:

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

    但是,我的建议是坚持使用 iOS 已经提供的标准 UI 选项。不要试图让您的应用程序的外观和行为与 Android 版本完全相同。想想如何重新打包它,让它看起来和感觉都像一个 iOS 应用程序。

    【讨论】:

    • 还有关于 Android Toast 的说明!好的!此信息可帮助来自 Android 开发的新开发人员。谢谢!
    • 伙计,我需要打印和装帧!你昨天和今天又救了我:D
    • 然后你就有了 Contextual Cards,就像连接到 AirPods 时显示的那样。 github.com/alexisakers/BulletinBoard
    • 这是什么解释。你是一个超级巨星。伟大的工作
    【解决方案4】:

    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”方法。

    【讨论】:

      【解决方案5】:

      对于 Swift 3 和 Swift 4:

      由于 UIAlertView 已被弃用,因此有一种在 Swift 3 上显示 Alert 的好方法

      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 版本:

      显示警报视图:

         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 现已弃用。
      【解决方案6】:

      这是 Xamarin.iOS 中的 C# 版本

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

      【讨论】:

        【解决方案7】:

        虽然我已经写了一个overview 不同类型的弹出窗口,但大多数人只需要一个警报。

        如何实现弹出对话框

        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)
            }
        }
        

        我更完整的答案是here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-02
          • 1970-01-01
          相关资源
          最近更新 更多