【问题标题】:Passing data in UIViewControllerRepresentable在 UIViewControllerRepresentable 中传递数据
【发布时间】:2019-10-12 07:46:57
【问题描述】:

我正在尝试使用 UIKit 制作 PDFView 并从以前的 SwiftUI 视图中传递数据。数据本身是一个带有文件名的字符串。不知何故,字符串没有从 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

   }
}

中的结果是: //----------------------- //-------------------------------

应该是: //----------------------- 细绳 //-------------------------------

【问题讨论】:

    标签: xcode swiftui pdfkit


    【解决方案1】:

    当您只想传递一个单独的 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>) {
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我想我已经成功了:

      func makeUIViewController(context: UIViewControllerRepresentableContext<FileViewerWrapper>) -> FileViewerWrapper.UIViewControllerType {
          let uiViewController = FileViewer()
          uiViewController.file = file
      
             return uiViewController
         }
      

      func updateUIViewController 现在为空

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-20
        • 2017-02-22
        • 2021-04-16
        相关资源
        最近更新 更多