以下代碼適用于在按下按鈕“Export Document1”后匯出一個 json 檔案(document1.json 的檔案名如圖所示)。但是在添加第二個檔案 document2.json 及其按鈕“Export Document2”后,每次它只匯出第一個檔案(document1.json)無論按下哪個按鈕。我是 DocumentInteraction 的新手,任何幫助都將提前感謝。這是代碼。謝謝。
struct DocumentInteraction: View {
@State private var isExportingDocument = false
var body: some View {
VStack {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url1 = dir.appendingPathComponent("document1.json")
Button("Export Document1") { self.isExportingDocument = true }
.background(DocumentInteractionController($isExportingDocument, url: url1))
}
VStack {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url2 = dir.appendingPathComponent("document2.json")
Button("Export Document2") { self.isExportingDocument = true }
.background(DocumentInteractionController($isExportingDocument, url: url2
))
}
}
}
struct DocumentInteraction_Previews: PreviewProvider {
static var previews: some View { if #available(iOS 14.0, *) {
DocumentInteraction()
} else {
// Fallback on earlier versions
} }
}
struct DocumentInteractionController: UIViewControllerRepresentable {
fileprivate var isExportingDocument: Binding<Bool>
fileprivate let viewController = UIViewController()
fileprivate let documentInteractionController: UIDocumentInteractionController
init(_ isExportingDocument: Binding<Bool>, url: URL) {
self.isExportingDocument = isExportingDocument
documentInteractionController = .init(url: url)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentInteractionController>) -> UIViewController { viewController }
func updateUIViewController(_ controller: UIViewController, context: UIViewControllerRepresentableContext<DocumentInteractionController>) {
if isExportingDocument.wrappedValue && documentInteractionController.delegate == nil {
documentInteractionController.uti = documentInteractionController.url?.typeIdentifier ?? "public.data, public.content"
documentInteractionController.name = documentInteractionController.url?.localizedName
documentInteractionController.presentOptionsMenu(from: controller.view.frame, in: controller.view, animated: true)
documentInteractionController.delegate = context.coordinator
documentInteractionController.presentPreview(animated: true)
}
}
func makeCoordinator() -> Coordintor { Coordintor(self) }
}
uj5u.com熱心網友回復:
當您通過按下按鈕將 isExportingDocument 設定為 true 時,將為每個按鈕觸發后臺任務。他們需要有不同的狀態來控制它們。
struct DocumentInteraction: View {
@State private var isExportingDocument1 = false
@State private var isExportingDocument2 = false
var body: some View {
VStack {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url1 = dir.appendingPathComponent("document1.json")
Button("Export Document1") { self.isExportingDocument1 = true }
.background(DocumentInteractionController($isExportingDocument1, url: url1))
}
VStack {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url2 = dir.appendingPathComponent("document2.json")
Button("Export Document2") { self.isExportingDocument2 = true }
.background(DocumentInteractionController($isExportingDocument2, url: url2
))
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/443526.html
上一篇:如何使用AndroidAudioManager使用QAndroidJniObject設定SpeakerphoneOn(true)?
下一篇:AndroidStudioBumblebee中的Git或Githubpull、push錯誤解決方案|天天要聞2021.1.1
