简体   繁体   English

将变量从 UIViewController 传递给 SwiftUI 视图

[英]Pass variabile from UIViewController to SwiftUI View

I can't find a way or a good tutorial to explain how to pass the value of a variable (String or Int) that is owned by a UIViewController to a SwiftUI view that is calling the view.我找不到一种方法或好的教程来解释如何将 UIViewController 拥有的变量(String 或 Int)的值传递给调用该视图的 SwiftUI 视图。

For example:例如:

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>) {
        
    }
    
}

In SwiftUI I'll have在 SwiftUI 我会有

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

How can I take myString of the ViewController and use it in ContentView?如何获取 ViewController 的 myString 并在 ContentView 中使用它? Thanks in advance提前致谢

Use MVVM pattern it is what is recommended with SwiftUI.使用 MVVM 模式,这是 SwiftUI 推荐的模式。

Share a ViewModel between your SwiftUI View and your UIKit ViewController .在 SwiftUI ViewUIKit ViewController之间共享一个 ViewModel 。

I suggest you start with the basic Apple SwiftUI tutorials.我建议你从基本的 Apple SwiftUI 教程开始。 Specifically how to "Interface with UIKit"具体如何“与 UIKit 交互”

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

import SwiftUI

struct SwiftUIView: View {
    @ObservedObject 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()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM