简体   繁体   English

SwiftUI - 从 SwiftUI 展示 ViewController/TableViewController

[英]SwiftUI - Presenting a ViewController/TableViewController from SwiftUI

The app is still heavily using ViewControllers and TableViewControllers, which is making it difficult to integrate SwiftUI.该应用程序仍然大量使用 ViewControllers 和 TableViewControllers,这使得集成 SwiftUI 变得困难。

Firstly, I'm presenting a swiftUI view from a ViewController.首先,我从 ViewController 展示一个 swiftUI 视图。

class SwiftUIViewController : UIViewController {

    let contentView = UIHostingController(rootView: SwiftUIView())

    override func viewDidLoad() {
        super.viewDidLoad()
    
        view.addSubview(contentView.view)
    }
}

Now, from within my SwiftUIView , I need to start ViewControllers and TableViewControllers.现在,在我的SwiftUIView中,我需要启动 ViewControllers 和 TableViewControllers。 I created an injector class that can be used with.sheet to present a view controller from SwiftUI.我创建了一个喷油器 class,它可以与.sheet 一起使用,以呈现来自 SwiftUI 的视图 controller。

struct ViewControllerInjector : UIViewControllerRepresentable {

    let viewController: UIViewController

    func makeUIViewController(context: Context) -> UIViewController {
        return viewController
    }
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
    
    }
}

Now this kind of works, the only issue is that I can't seem to find a way to present the ViewController without it being a .sheet现在这种作品,唯一的问题是我似乎无法找到一种方法来呈现 ViewController 而不是.sheet

struct SwiftUIView : View {
    @State private var showingVC1 = false
    ...

    .sheet(isPresented: $showingVC1) {
        ViewControllerInjector(viewController: ViewController1())
    }

    ...
}

I would like to completely leave the swift ui screen and continue with the view controller until I press the back button and return to the SwiftUI screen.我想完全离开 swift ui 屏幕并继续查看 controller 直到我按下后退按钮并返回 SwiftUI 屏幕。 Also, the injector doesn't seem to work with TableViewControllers even if I create a second injector class dedicated to TableView.此外,即使我创建了专用于 TableView 的第二个注入器 class,注入器似乎也无法与TableViewControllers一起使用。

Is this type of navigation possible?这种导航方式可行吗? If anything is not clear, don't hesitate to leave a comment.如果有任何不清楚的地方,请随时发表评论。 Thanks谢谢

Is the issue being that.sheet is a modal view and you would like it to be fullscreen?问题是 that.sheet 是一个模态视图,你希望它是全屏的吗? In that case, try replacing .sheet with .fullScreenCover在这种情况下,请尝试将.sheet替换为.fullScreenCover

To present a TableViewController in SwiftUI, try switching UIViewController to UITableViewController or UITableView.要在 SwiftUI 中显示 TableViewController,请尝试将 UIViewController 切换到 UITableViewController 或 UITableView。 Here's a code snippet I found here some time ago:这是我前段时间在这里找到的代码片段:

struct UITableViewRepresentable: UIViewRepresentable {

    func makeUIView(context: Context) -> UITableView {
   
        uiTableView.dataSource = context.coordinator
        uiTableView.delegate = context.coordinator

        }

        uiTableView.register(HostingCell.self, forCellReuseIdentifier: "Cell")
        return uiTableView
    }

    func updateUIView(_ uiTableView: UITableView, context: Context) {}

    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }

    class HostingCell: UITableViewCell { // just to hold hosting controller
        var host: UIHostingController<AnyView>?
    }

    class Coordinator: NSObject, UITableViewDelegate, UITableViewDataSource {

        init(_ parent: UITableViewRepresentable) {
           
        }

        func numberOfSections(in tableView: UITableView) -> Int {
            return self.parent.items.keys.count
        }

        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return parent.items[section]?.count ?? 0
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           
          
            return tableViewCell
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM