Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 11-UIDocumentBrowservicewController使用模板选择器创建新文档,如何设置?_Ios_Ios11 - Fatal编程技术网

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

iOS 11-UIDocumentBrowservicewController使用模板选择器创建新文档,如何设置?,ios,ios11,Ios,Ios11,我有一个现有的基于Objective-C文档的应用程序,我用新的UIDocumentBrowserveWController替换了以前的文件管理器,一切都正常——除了我完全不知道如何使用模板选择器创建新文档。根据WWDC 2017视频“在iOS 11中构建出色的基于文档的应用程序”,您应该这样做: func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHa

我有一个现有的基于Objective-C文档的应用程序,我用新的UIDocumentBrowserveWController替换了以前的文件管理器,一切都正常——除了我完全不知道如何使用模板选择器创建新文档。根据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)  
    }  
} 
    let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in
        completion(nil, true)
    }

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

我知道你要求最好使用Objective-C,但这是斯威夫特期权模式的一个例子。如果您的presentTemplateChooser方法在没有templateURL的情况下调用其完成闭包(即为零),则展开templateURL将失败(如果let templateURL=templateURL将返回false)

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

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)  
    }  
} 
    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 let templateURL = templateURL  
    {  
      importHandler(templateURL, .copy)  
    }  
    else  
    {  
      importHandler(nil, .none)  
    }  
} 
    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。(即,如果templateURL!=nil,而不是templateURL=templateURL)。

Hmm,这是否真的按预期工作?我正在尝试类似的方法(尽管是在Objective-C中),但是-documentBrowser:didRequestDocumentCreationWithHandler:instance方法的完成处理程序会在模板选择视图显示(“呈现”)后立即调用,而不是在用户在其中做出选择并将其取消后调用。我错过了什么?