![](/img/trans.png)
[英]SwiftUI - Navigate from UITableView (UIViewRepresentable) to View
[英]Present view from UIViewRepresentable in SwiftUI
我刚刚用UIViewRepresentable
创建了一个 UIKit tableView。
现在我需要呈现一个新的 SwiftUI 视图,如果选择了一个 tableCell。
这是我到目前为止所做的:
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 方法是否正确? 我怎样才能改进我的解决方案?
根据情况,你描述我不明白为什么你需要使用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)
}
}
}
请记住,只有在 SwiftUI 中尚未实现视图时, UIViewRepresentable
才有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.