在 UIViewControllerRepresentable 中传递数据 [英] Passing data in UIViewControllerRepresentable
问题描述
我正在尝试使用 UIKit 制作 PDFView 并从以前的 SwiftUI 视图中传递数据.数据本身是一个带有文件名的字符串.不知何故,字符串没有从 UIViewControllerRepresentable 传递到 UIViewController 并且它保持为空.我真的找不到发生这种情况的原因.你能检查一下我哪里错了吗?我希望这段代码足够了.
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.
我正在使用 NavigationLink(destination: FileViewerWrapper(file: "some string")) {}
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 //-------------------------------
推荐答案
当您只想传递使用单独的 UIViewControllerRepresentable
时,它将拥有您的 UIViewController,然后将数据从 View 传递到 ViewController.
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>) {
}
}
这篇关于在 UIViewControllerRepresentable 中传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!