简体   繁体   English

如何在 UIKit ViewController 中使用 EnvironmentObject

[英]How to use EnvironmentObject in UIKit ViewController

I'm building a SwiftUI app that wraps UIKit ViewController through UIViewControllerRepresentable .我正在构建一个 SwiftUI 应用程序,它通过UIViewControllerRepresentable包装 UIKit ViewController The goal is to display a UITextView in my SwiftUI app.目标是在我的 SwiftUI 应用程序中显示UITextView The implementation of this is simple, like this:这个的实现很简单,像这样:

// iOSTextView.swift
import SwiftUI

struct iOSTextView: UIViewControllerRepresentable {
    @Binding var document: Document
    @EnvironmentObject var userData: UserData
    
    func makeUIViewController(context: Context) -> some UIViewController {
        let view = iOSTextViewController()
        return view
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {

    }
}

Notice the EnvironmentObject named UserData .注意名为UserDataEnvironmentObject It's a class that holds some general data that should be shared with every view in my app, including the UITextView ViewController.它是一个 class ,它包含一些应该与我的应用程序中的每个视图共享的一般数据,包括 UITextView ViewController。

// UserData.swift
import Foundation

final class UserData: ObservableObject  {
    @Published var someValue = 1
}

Below is my iOSTextViewController ViewController:下面是我的iOSTextViewController

// iOSTextViewController.swift
import UIKit
import SwiftUI

class iOSTextViewController: UIViewController, UIGestureRecognizerDelegate {
    @EnvironmentObject var userData: UserData

    // Create a Text View
    let textView: UITextView = {
        let tv = UITextView()
        return tv
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Place the Text View on the view
        view.addSubview(textView)
        textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        textView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        textView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
        
        // create attributed string
        let string = "This is a string."
        var attributes: [NSMutableAttributedString.Key: Any] = [
            .foregroundColor: UIColor.red,
            .font: UIFont(name: "Courier", size: 12)!
        ]
        let myAttrString = NSMutableAttributedString(string: string, attributes: attributes)
        textView.attributedText = myAttrString
    }    
}

My questions:我的问题:

  1. How can I pass UserData to my ViewController named iOSTextViewController ?如何将UserData传递给名为iOSTextViewController的 ViewController ? Should I use a Coordinator?我应该使用协调员吗? How can I use it to make the EnvironmentObject available and synchronized inside my ViewController?如何使用它使EnvironmentObject在我的 ViewController 中可用和同步?

  2. In the same way, how can I share my document binding, since it's a document-based app?同样,我如何共享我的document绑定,因为它是基于文档的应用程序?

This can be accomplished in SwiftUI relatively easily, to pass your UserData variable to any object you can make a Published variable in a separate class.这可以在 SwiftUI 中相对容易地完成,将您的 UserData 变量传递给任何 object 您可以在单独的 class 中创建一个已发布的变量。 Here is an example这是一个例子

class UserDataManager: ObservableObject { 

     @Published var userData: UserData = UserData(variable1, variable2...)

}

Then in your View struct you can simply do然后在你的 View 结构中你可以简单地做

struct TextView: View { 
    
   @ObservedObject var userDataManager = UserDataManager()

   var body : some View {
     
      TextField("Enter your data", text: $userDataManager.userData.name)

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

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