如何在 Swift 5 中选择文件的屏幕上创建取消按钮?
How to create a Cancel button on the screen that selects the file on Swift5?
我正在使用 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)
}
}
我想要的取消按钮图片
帮了我很多
提前致谢。
我的理解是否正确,你想在导航堆栈上推送一个 viewController (documentViewController) 并在导航栏上有一个后退按钮,它会引导你回到你的主要 viewController (DocumentBrowserViewController) ?如果是这样,您首先需要将 documentViewController 推送到当前导航堆栈。
首先,documentViewController出现了吗?
我看到的是您实例化了一个 documentViewController,将其文档设置为 Document(...) 并结束了故事。我不使用情节提要,但实例化会显示 viewController 吗?
如果您提供更多详细信息,我会更新答案。但是一般的结论在你的presentDocument(...)中,你需要:
self.navigationController?.pushViewController(documentViewController, animated: true)
我了解了 UIDocumentBrowserViewController class 并成功添加了按钮。但是按钮的位置不是我想要的。
但这已经解决了我的根本问题,所以我会结束这个问题。
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)
}
我正在使用 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)
}
}
我想要的取消按钮图片
帮了我很多
提前致谢。
我的理解是否正确,你想在导航堆栈上推送一个 viewController (documentViewController) 并在导航栏上有一个后退按钮,它会引导你回到你的主要 viewController (DocumentBrowserViewController) ?如果是这样,您首先需要将 documentViewController 推送到当前导航堆栈。
首先,documentViewController出现了吗?
我看到的是您实例化了一个 documentViewController,将其文档设置为 Document(...) 并结束了故事。我不使用情节提要,但实例化会显示 viewController 吗?
如果您提供更多详细信息,我会更新答案。但是一般的结论在你的presentDocument(...)中,你需要:
self.navigationController?.pushViewController(documentViewController, animated: true)
我了解了 UIDocumentBrowserViewController class 并成功添加了按钮。但是按钮的位置不是我想要的。
但这已经解决了我的根本问题,所以我会结束这个问题。
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)
}