0

I have a Button in my View that call a custom View Controller (UIViewControllerRepresentable) because, the external library that I use, needs a view controller as parameter to show its own popups (and of course I cannot change).

For this reason I've created this middleware view controller but using this logic, the behaviour is not what i want...
Two popups, one on top of the other will be opened.

This because this library is not created thinking in SwiftUI, but the classic UIKit.


What I've done:

ContentView

struct ContentView: View {
    @State var showViewController = false

    var body: some View {
        VStack(alignment: .leading, content: {
            Button("SHOW VIEW CONTROLLER") {
                showViewController.toggle()
            }
        })
        .sheet(isPresented: $showViewController, content: {
            EmptyViewController()
        })
    }
}

EmptyController:

struct EmptyViewController: UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<EmptyViewController>) -> UIViewController {
        let vc = UIViewController()
        vc.view.backgroundColor = .systemRed

        let extLibVC = ExternalLibraryClass()
        extLibVC.doSomethingAndShowVC(from: vc) // issue here, because I need to pass VC
        
        return vc
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<EmptyViewController>) {}
}

This is the result in this case:

demo

There is a way to use directly the

let extLibVC = ExternalLibraryClass()
extLibVC.doSomethingAndShowVC(from: vc)

in the View without passing from another view controller and avoid multiple popups?

Basically I don't want to see the RED View controller, but directly the GREEN one.

elp
  • 8,021
  • 7
  • 61
  • 120
  • Do you have callback in ExternalLibraryClass that informs that it is closed/completed/finished? – Asperi Mar 05 '21 at 12:11
  • 1
    BTW, you can show it from rootViewController, using approach like in https://stackoverflow.com/a/63276688/12299030. – Asperi Mar 05 '21 at 12:12
  • Thanjs Asperi, I choose to use `SwiftUIWindowBinder` that permit me to allow tap and seems to works well. I'll try also `HostingWindowFinder`. Thanks a lot for pointing me to the right way. – elp Mar 05 '21 at 17:13

1 Answers1

0

Just use NavigationLink, this push new page into stack using inside NavigationView

struct test: View {
var body: some View {
    NavigationView{
        NavigationLink(
            destination: EmptyViewController()
                        .navigationBarTitle(Text("Title"), displayMode: .inline),
            label: {
                Text("SHOW VIEW CONTROLLER")
            })
    }
}

}

Mir
  • 411
  • 1
  • 7
  • 16
  • No Amir, I don't need to show in a different way the same controllers, I need to call: `extLibVC.doSomethingAndShowVC(from: vc)` because it show itself a new view controller. – elp Mar 05 '21 at 12:02