0

Button我有一个View调用自定义视图控制器(UIViewControllerRepresentable),因为我使用的外部库需要一个视图控制器作为参数来显示它自己的弹出窗口(当然我不能改变)。

出于这个原因,我创建了这个中间件视图控制器,但是使用这个逻辑,行为不是我想要的......
两个弹出窗口,一个在另一个之上将被打开。

这是因为这个库不是在 SwiftUI 中创建的,而是经典的 UIKit。


我做了什么:

内容视图

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()
        })
    }
}

空控制器:

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>) {}
}

这是这种情况下的结果:

演示

有一种方法可以直接使用

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

View没有从另一个视图控制器传递并避免多个弹出窗口的情况下?

基本上我不想看到RED View 控制器,而是直接看到GREEN控制器。

4

1 回答 1

0

只需使用NavigationLink,这会使用 NavigationView 将新页面推送到堆栈中

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

}

于 2021-03-05T11:54:51.657 回答