gpt4 book ai didi

xcode - 在 UIViewControllerRepresentable 中传递数据

转载 作者:行者123 更新时间:2023-12-04 13:00:33 24 4
gpt4 key购买 nike

我正在尝试使用 UIKit 制作 PDFView 并从以前的 SwiftUI View 中传递数据。数据本身是一个带有文件名的字符串。不知何故,字符串没有从 UIViewControllerRepresentable 传递到 UIViewController 并且它保持为空。我真的找不到发生这种情况的原因。你能检查一下我哪里错了吗?我希望这段代码足够了。

我正在使用 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

}
}

结果在:
//----------------------
//------------------------------

它应该是:
//----------------------
字符串
//------------------------------

最佳答案

当您只想通过使用单独的 UIViewControllerRepresentable它将拥有您的 UIViewController,然后将数据从 View 传递到 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>) {

}

}

关于xcode - 在 UIViewControllerRepresentable 中传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com