【问题标题】:How to present the error message when opening UIDocument?打开 UIDocument 时如何显示错误消息?
【发布时间】:2018-07-26 23:11:32
【问题描述】:

我有一个基于文档的 iOS 应用,当它第一次打开一个文档时,主视图控制器调用UIDocument.open

document.open { success in
    if success { ... set up UI ... }
    else { ??? }
}

这里的问题是,如果success 为假,我无权访问该错误。通常,Apple 的 API 会在这些情况下将可选的 Error 参数传递给回调,但由于某种原因,它们不会在这里。

我发现这个方法可以在我的应用程序的UIDocument 子类中重写:

override func handleError(_ error: Error, userInteractionPermitted: Bool) {

现在在该方法中,我有Error,但我无法轻松访问名为document.open 的视图控制器,我需要提供类似UIAlertController 的内容来显示错误消息。这个handleError 方法也在非主线程上调用。

看起来我需要通过在实例或全局变量中传递信息来进行协调。因为这似乎比 Apple 的通常设计更尴尬——我希望错误在 open 的完成处理程序中可用,我想我可能会遗漏一些东西。

是否有另一种推荐的方法来获取错误对象并向用户显示消息?

【问题讨论】:

  • 谁在调用 document.open?它是视图控制器吗?它在 AppDelegate 中吗?如果我得到更好的理解,我想我可以提供帮助。如果它只是一个视图控制器,您可以在视图控制器中覆盖该功能并能够显示警报。你能分享更多的代码吗?
  • 好的,我已经编辑了这个问题,希望能稍微提高清晰度。
  • 可能与 stackoverflow.com/questions/26554894/… 重复,除了当你是 UIDocument(不是 UIViewController)时如何呈现警报的问题之外,一切似乎都是渣滓。
  • 您不能从 AppDelegate 中获取根 ViewController 并使用它来显示警报吗?
  • 也许吧。当我尝试时,我遇到了一个错误,我想如果我需要讨论它应该是一个单独的问题。它说Warning: Attempt to present <UIAlertController: 0x12e82d200> on <MyApp.DocumentBrowserViewController: 0x12e021200> whose view is not in the window hierarchy!

标签: ios cocoa-touch ios11 uidocument


【解决方案1】:

罗伯,

如果你真的想“迅速”,你可以实现一个闭包来完全做到这一点,而不需要静态/全局变量。

我将首先定义一个枚举来模拟对 UIDocument 的 API 调用的成功和失败案例。通用的 Result 枚举是一种非常常见的方法。

enum Result<T> {
    case failure(Error)
    case success(T)
}

从那里我会在你的类中定义一个可选的闭包来处理 UIDocument.open 的结果

我要做的实现是这样的:

class DocumentManager: UIDocument {

    var onAttemptedDocumentOpen: ((Result<Bool>) -> Void)?

    func open(document: UIDocument){
        document.open { result in
            guard result else { return } // We only continue if the result is successful

            // Check to make sure someone has set a function that will handle the outcome
            if let onAttemptedDocumentOpen = self.onAttemptedDocumentOpen {
                onAttemptedDocumentOpen(.success(result))
            }
        }
    }

    override func handleError(_ error: Error, userInteractionPermitted: Bool) {
        // Check to make sure someone has set a function that will handle the outcome
        if let onAttemptedDocumentOpen = self.onAttemptedDocumentOpen {
            onAttemptedDocumentOpen(.failure(error))
        }
    }

}

然后我来自任何班级将使用 DocumentManager 你会做这样的事情:

class SomeOtherClassThatUsesDocumentManager {

    let documentManger = DocumentManager()

    let someViewController = UIViewController()

    func someFunction(){
        documentManger.onAttemptedDocumentOpen = { (result) in
            switch result {
            case .failure(let error):
                DispatchQueue.main.async {
                    showAlert(target: self.someViewController, title: error.localizedDescription)
                }

            case .success(_):
                // Do something
                return
            }
        }
    }
}

奖励:这是我编写的一个静态函数,用于在某些视图控制器上显示 UIAlertController

/** Easily Create, Customize, and Present an UIAlertController on a UIViewController

 - Parameters:
    - target: The instance of a UIViewController that you would like to present tye UIAlertController upon.
    - title: The `title` for the UIAlertController.
    - message: Optional `message` field for the UIAlertController. nil by default
    - style: The `preferredStyle` for the UIAlertController. UIAlertControllerStyle.alert by default
    - actionList: A list of `UIAlertAction`. If no action is added, `[UIAlertAction(title: "OK", style: .default, handler: nil)]` will be added.

 */
func showAlert(target: UIViewController, title: String, message: String? = nil, style: UIAlertControllerStyle = .alert, actionList: [UIAlertAction] = [UIAlertAction(title: "OK", style: .default, handler: nil)] ) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: style)
    for action in actionList {
        alert.addAction(action)
    }
    // Check to see if the target viewController current is currently presenting a ViewController
    if target.presentedViewController == nil {
        target.present(alert, animated: true, completion: nil)
    }
}

【讨论】:

  • 谢谢,我接受这个答案。我同意你的主要观点:给handleError 一个回调给调用UIDocument.open 的视图控制器。但是,我对DocumentManager 的设计感到不舒服。你不希望DocumentManager(使用新的open(doc) 方法)和Document 之间存在is-a 关系,而Document 已经有open() 方法。
  • 我想知道我的handleError() 是否应该调用超类实现?如果我应该打电话给finishedHandlingError(_:recovered:)
猜你喜欢
  • 1970-01-01
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
  • 2016-07-21
  • 1970-01-01
  • 2018-08-09
相关资源
最近更新 更多