将数据从 UIViewControllerRepresentable 传递给 UIViewController

Posted

技术标签:

【中文标题】将数据从 UIViewControllerRepresentable 传递给 UIViewController【英文标题】:Passing data from UIViewControllerRepresentable to UIViewController 【发布时间】:2020-03-17 20:37:51 【问题描述】:

我正在尝试从 SwiftUI 结构(名字和姓氏)传递数据,但似乎无法使用 UIViewControllerRepresentable 中的数据更新 UIViewController 中的变量。

我已检查并确认我尝试从 SwiftUI 视图传入的数据是正确的。我需要做什么/更改以更新 UIViewController 中的 firstName 和 lastName 变量?

import UIKit
import PDFKit
import SwiftUI

class PDFPreviewViewController: UIViewController 
    public var documentData: Data?

    var firstName: String = "firstName did not load"
    var lastName: String = "lastName did not load"

    @IBOutlet weak var pdfView: PDFView!

    override func viewDidLoad() 
        super.viewDidLoad()

        let pdfCreator = PDFCreator(firstName: firstName, lastName: lastName, format: "test format")
        documentData = pdfCreator.createReleasePDF()
        if let data = documentData 
            pdfView.document = PDFDocument(data: data)
            pdfView.autoScales = true
        
    


struct PDFPreviewController: UIViewControllerRepresentable 
    var release: ModelRelease
    let vc = PDFPreviewViewController()

    func makeUIViewController(context: UIViewControllerRepresentableContext<PDFPreviewController>) -> UIViewController 

        let storyboard = UIStoryboard(name: "Preview", bundle: Bundle.main)
        let controller = storyboard.instantiateViewController(identifier: "Preview")

        vc.firstName = release.firstName
        vc.lastName = release.lastName

        return controller
    

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<PDFPreviewController>) 
    


struct PDFPreviewControllerWrapper: View 
    @Environment(\.presentationMode) var presentationMode

    var release: ModelRelease

    var body: some View 
        NavigationView 
            PDFPreviewController(release: release)
            .navigationBarTitle(Text("Preview"))
            .navigationBarItems(trailing: Button(action: 
                self.presentationMode.wrappedValue.dismiss()
            ) 
                Text("Done")
                    .fontWeight(.bold)
                
            )
        
    


【问题讨论】:

【参考方案1】:

创建可表示的视图控制器,您将参数设置到一个控制器但返回另一个控制器。可能您的意思是:

//let vc = PDFPreviewViewController() // don't think it is needed at all

func makeUIViewController(context: UIViewControllerRepresentableContext<PDFPreviewController>) -> UIViewController 

    let storyboard = UIStoryboard(name: "Preview", bundle: Bundle.main)
    let controller = storyboard.instantiateViewController(identifier: 
                         "Preview") as! PDFPreviewViewController

    controller.firstName = release.firstName // << here !
    controller.lastName = release.lastName   // << and here !

    return controller

【讨论】:

当我进行此更改时,我收到错误 Value of type 'UIViewController' has no member 'firstName' lastName 的错误 @Dwillmore,已更新。实际上它是您的控制器,您现在应该是哪种类型。 =)

以上是关于将数据从 UIViewControllerRepresentable 传递给 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章

将数据从 Amazon Redshift 迁移到 DynamoDB

通过消除重复将数据从文本文件加载到mysql数据库

如何将数据直接从 Excel 插入 Oracle 数据库

如何将数据从mongodb导入hive

将数据从 redshift 传输到 postgresql

将数据从 MVC 发布到 WebApi