【问题标题】:How to create a Cancel button on the screen that selects the file on Swift5?如何在 Swift 5 中选择文件的屏幕上创建一个取消按钮?
【发布时间】:2020-02-13 00:17:27
【问题描述】:

我正在使用 UIDocumentBrowser 来检索文件。但我无法在导航栏中放置返回或取消按钮。

我想为此制作一个取消按钮,但我无法制作取消按钮。我该如何解决这个问题?

当前代码

import Foundation
import UIKit

@available(iOS 11.0, *)
class DocumentBrowserViewController : UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
         delegate = self
        
         browserUserInterfaceStyle = .dark
         view.tintColor = .white
    }
        
    func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {
        let newDocumentURL: URL? = nil
        
        // Set the URL for the new document here. Optionally, you can present a template chooser before calling the importHandler.
        // Make sure the importHandler is always called, even if the user cancels the creation request.
        if newDocumentURL != nil {
            importHandler(newDocumentURL, .move)
        } else {
            importHandler(nil, .none)
        }
    }
    
    func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
        guard let sourceURL = documentURLs.first else { return }
        
        do{
           try presentDocument(at: sourceURL)
        } catch {
            Log.Debug("\(error)")
        }
    }
    
    func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
        // Present the Document View Controller for the new newly created document
        do{
            try presentDocument(at: sourceURL)
        } catch {
            Log.Debug("\(error)")
        }
    }
    
    func documentBrowser(_ controller: UIDocumentBrowserViewController, failedToImportDocumentAt documentURL: URL, error: Error?) {
        // Make sure to handle the failed import appropriately, e.g., by presenting an error message to the user.
    }
    
    func presentDocument(at documentURL: URL) throws {
        guard documentURL.startAccessingSecurityScopedResource() else {
            throw IXError.fileAcessFailed
        }
        
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let documentViewController = storyBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        
        documentViewController.document = Document(fileURL: documentURL)
    }
}

我想要的取消按钮图片

帮了我很多忙

提前致谢。

【问题讨论】:

    标签: ios swift file document


    【解决方案1】:

    了解了 UIDocumentBrowserViewController 类,并成功添加了按钮。但是按钮的位置不是我想要的。

    但这已经解决了我的基本问题,所以我将结束这个问题。

        override func viewDidLoad() {
            super.viewDidLoad()
            delegate = self
            allowsDocumentCreation = false
            allowsPickingMultipleItems = false
            browserUserInterfaceStyle = .dark
            view.tintColor = .white
            let cancelbutton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelButton(sender:)))
            additionalTrailingNavigationBarButtonItems = [cancelbutton]
        }
    
        @objc func cancelButton(sender: UIBarButtonItem) {
            dismiss(animated: true, completion: nil)
        }
    
    

    【讨论】:

      【解决方案2】:

      我是否正确理解您想要在导航堆栈上推送一个 viewController (documentViewController) 并在 navigationBar 上有一个返回按钮来引导您返回主 viewController (DocumentBrowserViewController)?如果是这样,首先您需要将 documentViewController 推送到当前导航堆栈上。

      首先是documentViewController出现了吗?

      我看到的是您实例化了一个 documentViewController,将它的文档设置为 Document(...) 并结束。我不使用情节提要,但实例化是否呈现 viewController?

      如果您提供更多详细信息,我将更新答案。但一般结论在您的 presentDocument(...) 中,您需要:

      self.navigationController?.pushViewController(documentViewController, animated: true)
      

      【讨论】:

      • 我不明白你的意思,因为我不懂 Swift 和 iOS 的语言。我把这段代码放在哪里?
      • 我通过插入我想要的图片来编辑问题。而我目前的功能是选择文件并导航到代码中的视图控制器屏幕。
      • 功能没有问题,但是进入文件选择画面时没有取消按钮。
      猜你喜欢
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 2021-11-01
      • 1970-01-01
      • 2012-10-07
      • 2016-11-30
      相关资源
      最近更新 更多