【问题标题】:Pass variable from UIViewController to SwiftUI View将变量从 UIViewController 传递给 SwiftUI View
【发布时间】:2021-01-31 19:51:06
【问题描述】:

我找不到解释如何将 UIViewController 拥有的变量(String 或 Int)的值传递给调用视图的 SwiftUI 视图的方法或好的教程。

例如:

class ViewController: UIViewController {
    var myString : String = "" // variable of interest
    ....
    
    func methodThatChangeValueOfString(){
        myString = someValue
    }
}

// to make the view callable on SwiftUI
extension ViewController: UIViewControllerRepresentable {
    typealias UIViewControllerType = ViewController
    
    public func makeUIViewController(context: UIViewControllerRepresentableContext<ViewController>) -> ViewController {
        return ViewController()
    }
    
    func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext<ViewController>) {
        
    }
    
}

在 SwiftUI 中我将拥有

struct ContentView: View {
    var body: some View {
        ViewController()
    }
}

如何获取 ViewController 的 myString 并在 ContentView 中使用它? 提前致谢

【问题讨论】:

  • 有几种方法,但目标不是很明确。
  • 好的,我会解释我的具体问题。在 ViewController 中,我有摄像头,并使用 Vision 进行对象检测。相机检测到的对象的名称作为字符串保存在变量中。我想使用 SwiftUI 在屏幕上打印字符串,但我无法将数据传递给我的 SwiftUI 视图。

标签: ios data-binding uiviewcontroller swiftui uikit


【解决方案1】:

SwiftUI 推荐使用 MVVM 模式。

在您的 SwiftUI View 和您的 UIKit ViewController 之间共享一个 ViewModel。

我建议您从基本的 Apple SwiftUI 教程开始。具体如何“与 UIKit 交互”

https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit

import SwiftUI

struct SwiftUIView: View {
    @StateObject var sharedVM: SharedViewModel = SharedViewModel()
    var body: some View {
        VStack{
            UIKitViewController_UI(sharedVM: sharedVM)
            Text(sharedVM.myString)
        }
    }
}

class SharedViewModel: ObservableObject{
    @Published var myString = "init String"
}
//Not an extension
struct UIKitViewController_UI: UIViewControllerRepresentable {
    typealias UIViewControllerType = UIKitViewController
    var sharedVM: SharedViewModel
    
    func makeUIViewController(context: Context) -> UIKitViewController {
        return UIKitViewController(vm: sharedVM)
    }
    
    
    func updateUIViewController(_ uiViewController: UIKitViewController, context: Context) {
        
    }
    
}
class UIKitViewController: UIViewController {
    let sharedVM: SharedViewModel
    
    var runCount = 0
    
    init(vm: SharedViewModel) {
        self.sharedVM = vm
        super.init(nibName: nil, bundle: nil)
        //Sample update mimics the work of a Delegate or IBAction, etc
        Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
            self.runCount += 1
            self.methodThatChangeValueOfString()
            if self.runCount == 10 {
                timer.invalidate()
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func methodThatChangeValueOfString(){
        sharedVM.myString = "method change" + runCount.description
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

【讨论】:

  • 有没有办法在 View 结构中创建 UIKitViewController_UI 作为变量,然后传递给 sharedVM?我需要从视图中调用UIKitViewController_UI 中的一些函数。但是,当我尝试执行此操作时,它会调用 'self' used before all stored properties are initialized 错误。
  • @Björn 你可以创建它,但它可能无法正常工作,因为它会在一个类中 SwiftUI 视图应该保留在结构中。提出问题,人们可以查看您的 Minimal Reproducible Example,但听起来您只是设置变量太早了。
猜你喜欢
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 2020-03-26
相关资源
最近更新 更多