【问题标题】:SwiftUI fileExporter does not present when used as a button modifier within a toolbar modifier of DocumentGroupSwiftUI fileExporter 在 DocumentGroup 的工具栏修饰符中用作按钮修饰符时不存在
【发布时间】:2021-12-17 15:49:56
【问题描述】:

在 iOS 的特定场景下,我无法让 .fileExporter 在 SwiftUI 中呈现:当用作 DocumentGroup 场景工具栏中的按钮修饰符时。

请参阅下面显示问题的代码。

预期结果:当用户按下“保存文件”按钮时,应该出现 .fileExporter 并提示用户保存文件。

实际结果:即使状态变量已更改且文档不为零,似乎也没有发生任何事情。

问题出现在 iOS 15.0 模拟器中,iOS 15.1 和 15.2 使用硬件。这不会出现在 macOS 12.0 中。

还有其他人遇到过这个问题吗?有已知的解决方法吗?


import SwiftUI

@main
struct myApp: App {
    
    @State private var showingFileExporter = false

    var body: some Scene {
        DocumentGroup(newDocument: myDocument()) { file in
            ContentView(document: file.$document)
                .toolbar {
                    Button("Save File") {
                        showingFileExporter = true
                    }
                    .fileExporter(isPresented: $showingFileExporter,
                                  document: file.document,
                                  contentType: UTType.exampleText) { result in
                        }
                }
        }
    }
}


import UniformTypeIdentifiers

extension UTType {
    static var exampleText: UTType {
        UTType(importedAs: "com.example.plain-text")
    }
}

struct myDocument: FileDocument {
    var text: String

    init(text: String = "Hello, world!") {
        self.text = text
    }

    static var readableContentTypes: [UTType] { [.exampleText] }

    init(configuration: ReadConfiguration) throws {
        guard let data = configuration.file.regularFileContents,
              let string = String(data: data, encoding: .utf8)
        else {
            throw CocoaError(.fileReadCorruptFile)
        }
        text = string
    }
    
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let data = text.data(using: .utf8)!
        return .init(regularFileWithContents: data)
    }
}


struct ContentView: View {
    @Binding var document: myDocument
    var body: some View {
        TextEditor(text: $document.text)
    }
}


【问题讨论】:

  • 如果您将工具栏添加到ContentView 结构而不是应用结构,文件导出器是否会出现?您可能必须将文本编辑器包装在堆栈或导航视图中才能在内容视图中应用 .toolbar 修饰符。如果文件导出器没有出现,您可能需要在 app 结构中对 ContentView 的调用中添加 .focusedSceneValuefocusedValue 修饰符,并将文档作为参数提供。
  • 不,fileExporter 放置在带有某种堆栈的内容视图中时不会显示。我无法在任何专注的状态下进行计算。如果我将 .fileExporter 移到 .toolbar 修饰符之外,我能够显示它。这可能是一种可能的解决方法,但对我来说似乎有点混乱;最好在工具栏中有视图,以便能够拥有自己的 fileExporter 修饰符。
  • 我查看了一些文件导出器代码,发现.fileExporter 修饰符在.toolbar 修饰符之外。这可能是您需要做的,但也许其他人会为您提供更好的解决方案。

标签: ios swiftui


【解决方案1】:

我的 fileImporter 也遇到了同样的问题,如果你将它移到我们的 .toolbar 上下文中,它确实可以工作

这是我的代码:

 .toolbar {
            Button("Import") {
                presentImporter.toggle()
            }
    }
    .fileImporter(isPresented: $presentImporter, allowedContentTypes: [.mp3]) { result in
        switch result {
        case .success(let url):
            print(url)
            
            let newBook = Book(context: viewContext)
            newBook.name = "\(url.lastPathComponent)"
            newBook.url = url
            newBook.origin = playlist.self
            
            try? viewContext.save()
            let _ = print("New book", newBook.name as Any)
            let _ = print("inside", newBook.origin as Any)
            
        case .failure(let error):
            print(error)
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2021-06-12
    • 2020-11-06
    • 2020-01-30
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    相关资源
    最近更新 更多