Saturday, October 14, 2023
HomeiOS DevelopmentTips on how to get setting object in UIViewController?

Tips on how to get setting object in UIViewController?


I write SwiftUI module, however there’s a characteristic that makes use of UIKit performance. So in my SwiftUI view there’s a UIViewRepresentable wrapper, that makes UIKit controller. In its flip it makes a group view the place view is SwiftUI view.

So I’ve a series like this: View -> ViewRepresentable -> CollectionView -> View

And the issue is that I’ve @State variable within the first View, and I wish to entry and alter it within the final view. How can I do it?

FirstView.swift

...

@ObservedObject var viewModel: ViewModel

struct FirstView: View {

    var physique: some View {
        CollectionWrapper()
            .environmentObject(viewModel)
    }
}
...

Wrapper.swift

...
struct CollectionWrapper: UIViewRepresentable {
    @EnvironmentObject var viewModel: Mannequin

    func makeUIView(context: Context) -> CollectionView {
        CollectionView() // how I can cross the variable? DI will not be truly working proper.
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
    }
}
...

CollectionView.swift

...
class CollectionView: UICollectionView {
    ...

    func makeView() -> UIView {
        // Make internet hosting controller and so on.
        hostingContoller.view = ModelView() // and right here I someway ought to cross my view mannequin
    }
    ...
}
...

I’ve already tried to do DI by way of the wrapper in makeUIView methodology, however my app is crashing as a result of .environmentObject does not work in internet hosting controller for some cause.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments