首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在打开UIDocument时显示错误信息?

如何在打开UIDocument时显示错误信息?
EN

Stack Overflow用户
提问于 2018-07-27 07:11:32
回答 1查看 381关注 0票数 2

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

代码语言:javascript
复制
document.open { success in
    if success { ... set up UI ... }
    else { ??? }
}

这里的问题是,如果success为false,我就无法访问错误。在这些情况下,苹果的API通常会将一个可选的Error参数传递给回调函数,但由于某种原因,这里不会这样做。

我找到了这个方法,我可以在应用程序的UIDocument子类中覆盖它

代码语言:javascript
复制
override func handleError(_ error: Error, userInteractionPermitted: Bool) {

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

看起来我需要通过在实例或全局变量中传递信息来进行协调。因为这似乎比苹果通常的设计更笨拙--我预计open的补全处理程序会出现这个错误,所以我想我可能遗漏了什么。

有没有其他推荐的方法来获取错误对象并向用户显示一条消息?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-28 07:21:23

罗伯

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

我将从定义一个枚举开始,该枚举对UIDocument的成功和失败案例进行建模。泛型结果枚举是一种非常常见的方法。

代码语言:javascript
复制
enum Result<T> {
    case failure(Error)
    case success(T)
}

在此基础上,我将在您的类中定义一个可选闭包,用于处理UIDocument.open的结果

我要做的实现是这样的:

代码语言:javascript
复制
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,你会这样做:

代码语言:javascript
复制
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

代码语言:javascript
复制
/** 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)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51548675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档