问题
I'm trying to make a PDFView with UIKit and pass it data from a previous SwiftUI view. The data itself is a string with the name of the file. Somehow, the string is not passed from the UIViewControllerRepresentable to the UIViewController and it stays empty. I really can't find a reason why this is happening. Could you check where I'm wrong? I hope this code is enough.
I'm using NavigationLink(destination: FileViewerWrapper(file: "some string")) {}
import PDFKit
import UIKit
import SwiftUI
let pdfView = PDFView()
class FileViewer: UIViewController {
var file = String()
override func viewDidLoad() {
super.viewDidLoad()
print("\n\n\n-----------------------\(file)\n-------------------------------\n\n\n")
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
guard let path = Bundle.main.url(forResource: file, withExtension: "pdf", subdirectory: "Files") else { return }
if let document = PDFDocument(url: path) {
pdfView.document = document
}
}
}
struct FileViewerWrapper: UIViewControllerRepresentable {
var file: String
typealias UIViewControllerType = FileViewer
func makeUIViewController(context: UIViewControllerRepresentableContext<FileViewerWrapper>) -> FileViewerWrapper.UIViewControllerType {
return FileViewer()
}
func updateUIViewController(_ uiViewController: FileViewerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<FileViewerWrapper>) {
uiViewController.file = file
//When I put print(file) here, it's fine
}
}
The outcome in is: //----------------------- //-------------------------------
It should be: //----------------------- String //-------------------------------
回答1:
I think I've nailed it:
func makeUIViewController(context: UIViewControllerRepresentableContext<FileViewerWrapper>) -> FileViewerWrapper.UIViewControllerType {
let uiViewController = FileViewer()
uiViewController.file = file
return uiViewController
}
func updateUIViewController is now empty
回答2:
When you just want to pass the use a separate UIViewControllerRepresentable
which will have your UIViewController and then pass the data from View to ViewController.
struct CameraViewControllerRepresentable : UIViewControllerRepresentable {
typealias UIViewControllerType = CameraViewController
var patientObject: PatientObj
public func makeUIViewController(context: UIViewControllerRepresentableContext<CameraViewControllerRepresentable>) -> CameraViewController {
let cameraVC = UIStoryboard(name: "Camera", bundle: nil).instantiateViewController(identifier: String(describing: CameraViewController.self)) as! CameraViewController
cameraVC.patientObj = patientObject
return cameraVC
}
func updateUIViewController(_ uiViewController: CameraViewController, context: UIViewControllerRepresentableContext<CameraViewControllerRepresentable>) {
}
}
来源:https://stackoverflow.com/questions/58352016/passing-data-in-uiviewcontrollerrepresentable