I'm building a SwiftUI app that wraps UIKit ViewController
through UIViewControllerRepresentable
. The goal is to display a UITextView
in my SwiftUI app. 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
. It's a class that holds some general data that should be shared with every view in my app, including the UITextView ViewController.
// UserData.swift
import Foundation
final class UserData: ObservableObject {
@Published var someValue = 1
}
Below is my iOSTextViewController
ViewController:
// 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:
How can I pass
UserData
to my ViewController namediOSTextViewController
? Should I use a Coordinator? How can I use it to make theEnvironmentObject
available and synchronized inside my ViewController?In the same way, how can I share my
document
binding, since it's a document-based app?
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. Here is an example
Then in your View struct you can simply do