从UIRappresentable传递数据

时间:2020-03-22 10:20:47

标签: swiftui mac-catalyst

我有这个Catalyst for MacOS 应用,只需编写代码即可从本地磁盘读取json文件并查看文本中的数据。

import SwiftUI

final class PickerForReadFile: NSObject, UIViewControllerRepresentable, ObservableObject {
    @Published var geoFolder = GeoFolderCodStruct()

    lazy var viewController:UIDocumentPickerViewController = {
        let vc = UIDocumentPickerViewController(documentTypes: ["geof"], in: .open)
        vc.allowsMultipleSelection = false
        vc.delegate = self
        return vc
    }()

    func makeUIViewController(context: UIViewControllerRepresentableContext<PickerForReadFile>) -> UIDocumentPickerViewController {
        viewController.delegate = self
        return viewController
    }
}

extension PickerForReadFile: UIDocumentPickerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        if urls.count > 0 {
            DispatchQueue.main.async {
                let url = urls[0]
                do {
                    let data = try Data(contentsOf: url)
                    let decoder = JSONDecoder()
                    decoder.dateDecodingStrategy = .formatted(dateFormatter)
                    let jsonData = try decoder.decode(GeoFolderCodStruct.self, from: data)
                    self.geoFolder = jsonData
                    print("geoFolder: \(self.geoFolder.nomeArchivio)")
                } catch {
                    print("error:\(error)")
                }
            }
        }
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        controller.dismiss(animated: true) {
            print("Cancel from picker view controller")
        }
    }
}

private var dateFormatter: DateFormatter {
    let f = DateFormatter()
    f.dateStyle = .short
    return f
}


import SwiftUI

struct ContentView: View {

    @ObservedObject var picker = PickerForReadFile()
    @State private var geoFolder = GeoFolderCodStruct()

    var body: some View {
        VStack {
            Text("Hello, World!")
            Button(action: {
                #if targetEnvironment(macCatalyst)
                print("Press open file")
                let viewController = UIApplication.shared.windows[0].rootViewController!
                viewController.present(self.picker.viewController, animated: true)
                self.picker.objectWillChange.send()
                #endif
            }) {
                Image(systemName: "book")
            }
            Text(geoFolder.nomeCommittente).padding()
            Text(geoFolder.nomeArchivio).padding()
        }
    }
}

例如,我要读取的文件是:

{
  "nomeCommittente" : "Appple",
  "note" : "Image from Cupertino (CA).",
  "latitudine" : 37.332161,
  "longitudine" : -122.030352,
  "nomeCartella" : "Foto",
  "geoFolderPath" : "\/Users\/cesare\/Desktop",
  "radiusCircle" : 50,
  "dataCreazione" : "20\/03\/2020",
  "nomeArchivio" : "AppleCampus-Image",
  "isActive" : true
}

ContentView中,我试图插入此代码以更新geoFolder,但我不知道如何停止对私有func urlPickedFoRTextField() -> some View {的呼叫。

private func urlPickedFoRTextField() -> some View {
    DispatchQueue.main.async {
        let geoF = self.picker.geoFolder
        print("Committente: \(geoF.nomeCommittente) - Archivio: \(geoF.nomeArchivio)")
        self.geoFolder = geoF
        self.picker.objectWillChange.send()
    }
    return TextField("", text: $geoFolder.geoFolderPath)
}

为什么geoFolder不更新? 如何解决这个问题?如何在不调用任何功能的情况下更新geoFolder

0 个答案:

没有答案