How to create a Cancel button on the screen that selects the file on Swift5?

Viewed 460

I am using UIDocumentBrowser to retrieve files. But I am not able to place a back or cancel button in the navigation bar.

I want to make a cancellation button for this but I can't make a cancellation button. How can I solve this problem?

current code

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)
    }
}

picture of cancellation button that I want enter image description here

Help me a lot

Thanks in advance.

2 Answers

Do I understand correctly that you want to push a viewController (documentViewController) on the navigation stack and have a back button on the navigationBar that leads you back to your main viewController (DocumentBrowserViewController)? If so first you need to push documentViewController on the current navigation stack.

First of all, does the documentViewController appears?

What I see is that you instantiate a documentViewController, set it's document to Document(...) and end of story. I don't use storyboard but does instantiate presents the viewController?

If you provide more details I will update the answer. But general conclusion is in your presentDocument(...), you need:

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

I learned about the UIDocumentBrowserViewController class and succeeded in adding buttons. But the position of the button is not where I want it to be.

But this has solved my fundamental problem, so I'll end the question.

    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)
    }

enter image description here

Related