[ad_1]
Making an attempt to put a SwiftUI View right into a ViewController utilizing UIHostingController, however receiving the next error:
No ObservableObject of kind UIStateModel discovered. A View.environmentObject(_:) for UIStateModel could also be lacking as an ancestor of this view.
All assist appreciated, I have been fairly caught :(.
ViewController.swift
class ViewController: UIViewController {
@EnvironmentObject var UIModel: UIStateModel
var uiState: UIStateModel = UIStateModel()
override func viewDidLoad() {
tremendous.viewDidLoad()
addCarousel()
}
func addCarousel(){
let snapC : SnapCarousel
let contentView : UIHostingController<SnapCarousel>
uiState = UIStateModel()
snapC = SnapCarousel( UIState: uiState )
contentView = UIHostingController(rootView: snapC)
addChild(contentView)
view.addSubview(contentView.view)
contentView.didMove(toParent: self)
}
}
Carousel.swift
struct SnapCarousel: View {
var UIState: UIStateModel
//Carousel instantiation
}
struct Card: Decodable, Hashable, Identifiable {
//code
}
public class UIStateModel: ObservableObject {
@Revealed var activeCard: Int = 0
@Revealed var screenDrag: Float = 0.0
}
struct Carousel<Gadgets : View> : View {
@EnvironmentObject var UIState: UIStateModel
//card administration
}
struct Canvas<Content material : View> : View {
// view supervisor
}
struct Merchandise<Content material: View>: View {
@EnvironmentObject var UIState: UIStateModel
//code for card view
}
[ad_2]
