iOS 11 - UIDocumentBrowserViewController 使用模板选择器创建新文档,如何设置?

标签 ios ios11

我有一个现有的基于 Objective-C 文档的应用程序,我用新的 UIDocumentBrowserViewController 替换了我以前的文件管理器,一切正常——除了我完全不知道如何使用模板选择器创建新文档.根据 WWDC 2017 视频“在 iOS 11 中构建出色的基于文档的应用程序”,您应该这样处理:

func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Swift.Void) 
{  
presentTemplateChooser(completion: {templateURL, canceled) in  
    if let templateURL = templateURL  
    {  
      importHandler(templateURL, .copy)  
    }  
    else  
    {  
      importHandler(nil, .none)  
    }  
} 

对我来说有意义的是呈现模板选择器,但对我来说没有意义的是我在模板选择器上有一个“完成”和“取消”按钮;但是我怎么知道用户何时点击模板选择器上的“完成”或“取消”并将其传递给委托(delegate)函数?有人知道如何在(最好是)Objective-C 中实现这一点吗? (但 Swift 也很好,只是想了解一下这个过程是如何工作的)非常感谢。

最佳答案

我知道您最好使用 Objective-C,但这是 Swift 的可选模式的示例。如果您的 presentTemplateChooser 方法在没有 templateURL 的情况下调用其完成闭包(即它为 nil),则展开 templateURL 将失败(如果 let templateURL = templateURL 将返回 false)。

如果你想知道用户是否明确按下取消,你可以这样做:

像这样创建取消操作:

    let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in
        completion(nil, true)
    }

那么您问题中的方法应该是:

func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Swift.Void) 
{  
presentTemplateChooser(completion: {templateURL, canceled) in  
    if canceled {
      print("User canceled")
    }
    if let templateURL = templateURL  
    {  
      importHandler(templateURL, .copy)  
    }  
    else  
    {  
      importHandler(nil, .none)  
    }  
} 

您可以在 Objective-C 中完成所有这些。您只需要检查 nil 而不是解包 templateURL。 (即 if templateURL != nil 而不是 if templateURL = templateURL)。

关于iOS 11 - UIDocumentBrowserViewController 使用模板选择器创建新文档,如何设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45847146/

相关文章:

ios - UITableView 中的单元格重用问题

ios - 如何检测 iOS 11+ Safari 以及旧版本 Safari 中的隐私浏览?

ios - 如何在 iOS 11 上使用 HKWorkoutRouteQuery

ios - 如何更新 iOS PhotoScroller 示例代码

ios - MPMusicPlayerController 队列问题

swift - VNDetectRectanglesRequest 上的 AspectRatio : Can it handle width > height?

ios - SearchController SearchBar 宽度问题 iOS11

iOS 11-用户本地通知每 x 分钟重复一次

ios - Spotify EXC_BAD_EXE 在关闭 LoginViewController 后第二次点击登录按钮

ios - 等同于 AVPlayer 中的 MPMovieScalingMode?