【问题标题】:Present view from UIViewRepresentable in SwiftUI在 SwiftUI 中从 UIViewRepresentable 呈现视图
【发布时间】:2020-09-01 10:25:07
【问题描述】:

我刚刚使用UIViewRepresentable 创建了一个 UIKit tableView。

如果选择了 tableCell,现在我需要呈现一个新的 SwiftUI 视图。

这是我到目前为止所做的:

struct UIKitTableView: UIViewRepresentable {

    @Binding var showDetail: Bool
func makeCoordinator() -> Coordinator {
    Coordinator(showDetail: self.$showDetail)
}
class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {

    @Binding var showDetail: Bool

    /// Did select row at
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.showDetail = true
    }

现在在我的 SwiftUI 视图中,我可以显示视图:

NavigationLink(destination: CellDetail(), isActive: self.$showDetail) {

但我还需要传递相关单元格的数据,我想知道哪种方法最好。

我的另一个问题是:@Binding showDetail 方法是否正确?如何改进我的解决方案?

【问题讨论】:

  • 您找到解决方案了吗?

标签: ios uitableview swiftui uikit segue


【解决方案1】:

根据情况,你描述的我不明白你为什么需要使用UIViewRepresentable,因为SwiftUI已经在List的名称下有一个很好的实现

这里有一段代码可以帮助你显示详细视图:

import SwiftUI

struct MasterView: View {
    private let cells = [
        "Cell 1", "Cell 2", "Cell 3"
    ]

    var body: some View {
        NavigationView {
            List(cells, id: \.self) { cell in
                NavigationLink(destination: DetailsView(content: cell)) { // Creates all the logic to show detail view.
                    Text(cell)
                }
            }.navigationBarTitle("List")
        }
    }
}

struct DetailsView: View {
    let content: String

    var body: some View {
        VStack {
            Text(content)
                .font(.largeTitle)
        }
    }
}

请记住,UIViewRepresentable 仅在 SwiftUI 中尚未实现视图时才有帮助。

【讨论】:

  • 我已经知道我们有 List。但是由于 SwiftUI List 中没有实现尾随和前导动作,所以我选择了 UIViewRepresentable。
  • 那我建议你使用 Introspect 库
猜你喜欢
  • 2021-02-08
  • 2021-07-29
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 2020-09-06
相关资源
最近更新 更多