簡體   English   中英

將變量從 UIViewController 傳遞給 SwiftUI 視圖

[英]Pass variabile from UIViewController to SwiftUI View

我找不到一種方法或好的教程來解釋如何將 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 中使用它? 提前致謝

使用 MVVM 模式,這是 SwiftUI 推薦的模式。

在 SwiftUI ViewUIKit ViewController之間共享一個 ViewModel 。

我建議你從基本的 Apple SwiftUI 教程開始。 具體如何“與 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