【问题标题】:Modal pop up in swift 4swift 4中的模态弹出窗口
【发布时间】:2018-01-10 22:14:55
【问题描述】:

我最近一直在互联网上搜索用于 iOS 编程的模态对话框的实现。

我知道 UIAlertViewController。事实上,我目前正在一个应用程序中使用这个实现。但是,我需要一些模态的东西。我需要代码停止执行,直到用户单击警报中的 a 按钮。

到目前为止,我还没有真正看到任何令我满意的实现。这是我的代码的当前状态:

func messageBox(messageTitle: String, messageAlert: String, messageBoxStyle: UIAlertControllerStyle, alertActionStyle: UIAlertActionStyle)
    {
        var okClicked = false
        let alert = UIAlertController(title: messageTitle, message: messageAlert, preferredStyle: messageBoxStyle)

        let okAction = UIAlertAction(title: "Ok", style: alertActionStyle)
        {
            (alert: UIAlertAction!) -> Void in
            okClicked = true
        }


    /*    alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: alertActionStyle, handler: { _ in
            okClicked = true
            NSLog("The \"OK\" alert occured.")
        }))*/

        alert.addAction(okAction)

        self.present(alert, animated: true, completion: nil)
        while(!okClicked)
        {

        }
    }

我应该考虑在情节提要中创建自己的对话框硬核风格还是 swift 有某种我可以使用的实现?

【问题讨论】:

  • 为什么需要执行才能停止?为什么你不能在处理程序中为 OK 按钮做需要的事情?
  • 使用 while() 听起来不太好。必要时使用完成块。
  • 你在任何情况下都不应该做这样的事情。虽然您的应用程序在显示警报时可能什么都不做,但它仍应具有响应能力。您尝试这样做的原因是什么?

标签: ios swift dialog modal-dialog


【解决方案1】:

如果您想要自己的辅助函数,但只想在单击确定按钮后执行代码,您应该考虑向辅助函数添加完成处理程序。这是一个例子:

func messageBox(messageTitle: String, messageAlert: String, messageBoxStyle: UIAlertControllerStyle, alertActionStyle: UIAlertActionStyle, completionHandler: @escaping () -> Void)
    {
        let alert = UIAlertController(title: messageTitle, message: messageAlert, preferredStyle: messageBoxStyle)

        let okAction = UIAlertAction(title: "Ok", style: alertActionStyle) { _ in
            completionHandler() // This will only get called after okay is tapped in the alert
        }

        alert.addAction(okAction)

        present(alert, animated: true, completion: nil)
    }

我已从您的函数中删除了不需要的代码,并添加了一个完成处理程序作为最后一个参数。这个完成处理程序基本上是一个函数,您可以随时调用它,在这种情况下,我们在点击确定按钮时调用它。以下是您如何使用该功能:

viewController.messageBox(messageTitle: "Hello", messageAlert: "World", messageBoxStyle: .alert) {
    print("This is printed into the console when the okay button is tapped")
}

() -> Void 表示“一个不带参数、不返回值的函数”,@escaping 表示该函数将在以后异步调用,在这种情况下,我们从警报操作的点击按钮时的处理程序。

【讨论】:

    【解决方案2】:
    // declare an alert 
    let alert = UIAlertController(title: <#Your Title#>, message:  <#Your Message#> preferredStyle: UIAlertControllerStyle.actionSheet)
    //create action and add them to alert
    let actionX = UIAlertAction(title:  <#Your Title#>, style: UIAlertActionStyle.default, handler: {(action) in
         // do whatever you want  
    }))
    alert.addAction(actionX)
    

    您应该查看闭包以了解如何在 swift 中轻松编写代码

    【讨论】:

    • 美元符号有点令人困惑,因为它们在 Swift 中用于不同的事物。使用占位符字符串或实际的 Xcode 占位符可能会很好,例如:&lt;#Your Title#&gt;,所以当它被复制到 Xcode 中时,它会变成一个占位符:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多