I am hosting a SwiftUI view inside a view controller. My SwiftUI view has a @Binding data type.
While setting the content view, I need to initialize and set a binding variable.
My SwiftUI View:
struct STSChooseItemsView: View {
@Environment(\.presentationMode) var presentationMode
@Binding var selectedImage : UIImage
var body: some View {
VStack{
HStack(){
Spacer()
.frame(width: 5)
Button(action: {
presentationMode.wrappedValue.dismiss()
self.selectedImage = UIImage()
}, label: {
Text("X")
.fontWeight(.heavy)
.foregroundColor(Color.black)
})
Spacer()
.frame(width: 100)
Text("CHOOSE ITEMS")
.font(.headline)
.padding()
Spacer()
.frame(width: 100)
}
.frame(height: 40)
Spacer()
Image(uiImage: selectedImage)
.frame(width: UIScreen.main.bounds.width - 1, height: 300)
Spacer()
}
}
My view controller:
import UIKit
import SwiftUI
class STSChooseItemsViewController : UIViewController {
let contentView = UIHostingController(rootView: STSChooseItemsView(selectedImage: **(Please let me know what and how to put data here)**))
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "CHOOSE ITEMS"
addChild(contentView)
view.addSubview(contentView.view)
setupConstraints()
// Do any additional setup after loading the view.
}
fileprivate func setupConstraints(){
contentView.view.translatesAutoresizingMaskIntoConstraints = false
contentView.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
contentView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
contentView.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
contentView.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
}
}
I tried initializing the dummy image but I got an error: Cannot use instance member '$myImage' within property initializer; property initializers run before 'self' is available
CodePudding user response:
Try creating a Binding
using the init(get:set:)
initializer.
class STSChooseItemsViewController: UIViewController {
var image = UIImage()
lazy var selectedImageBinding = Binding { /// I omitted the argument label using trailing closure shorthand
return self.image
} set: { newValue in
self.image = newValue /// `selectedImageBinding` must be a lazy var to access `self`
}
lazy var contentView = UIHostingController(rootView: STSChooseItemsView(selectedImage: selectedImageBinding) /// pass in the binding here
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "CHOOSE ITEMS"
addChild(contentView)
view.addSubview(contentView.view)
setupConstraints()
// Do any additional setup after loading the view.
}
fileprivate func setupConstraints() {
contentView.view.translatesAutoresizingMaskIntoConstraints = false
contentView.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
contentView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
contentView.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
contentView.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
}
}