macos - List 中的 SwiftUI NavigationLinks 加载 NSViewDelegate/NSViewControllerDelegate 两次
问题描述
我目前正在涉足 macOS 开发,并尝试将一些 AppKit 组件包装到 SwiftUI 中。我正在使用带有NavigationLink
s的侧边栏导航List
。当使用NSViewRepresentable
orNSViewControllerRepresentable
作为目的地时,makeNSView
/makeNSViewController
方法被调用两次。我想这是一个错误,因为当我将NavigationLink
s 直接包装在NavigationView
? 有没有其他人遇到这个问题并且可以帮助我解决问题,直到它被修复?
重现的最小实现:
import SwiftUI
struct ViewRepresentable: NSViewRepresentable {
let title: String
func updateNSView(_ nsView: NSView, context: Context) {
}
func makeNSView(context: Context) -> NSView {
print("\(title) make view called")
return NSView()
}
}
struct ContentView: View {
enum NavigationItem {
case sel1
case sel2
}
@State private var selection: NavigationItem? = .sel1
var body: some View {
NavigationView {
List(selection: $selection) {
NavigationLink(
destination: EmptyView().navigationTitle("Dashboard"),
tag: NavigationItem.sel1,
selection: $selection
) {
Label("Dashboard", systemImage: "globe")
}.tag(NavigationItem.sel1)
NavigationLink(
destination: ViewRepresentable(title: "TestView").navigationTitle("TestView"),
tag: NavigationItem.sel2,
selection: $selection
) {
Label("Dashboard", systemImage: "globe")
}.tag(NavigationItem.sel2)
}
.padding(.top, 16)
.navigationTitle("SomeApp")
.listStyle(SidebarListStyle())
}.frame(minWidth: 1000, idealWidth: 1200, maxWidth: .infinity, minHeight: 600, idealHeight: 800, maxHeight: .infinity, alignment: .center)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
解决方案
推荐阅读
- python - 当我不使用任何 numpy 操作时,为什么它说我无法将符号张量转换为 numpy 数组?
- excel - 返回与下拉列表匹配的一组单元格
- android - Flutter 在从背景到前景时显示异常行为
- mongodb - 排除猫鼬中的文档列表
- node.js - Express 的 req.url 有时包含完整的 URL(包括协议+主机)而不仅仅是路径
- arrays - 在 Common Lisp 中映射数组时,两个 cons 指向相同的内存
- java - 为什么 Java 不强制将 final 与枚举属性一起使用
- sql - 如何使用 SQL WHERE 子句将值插入新表进行匹配?
- java - XML 到 JSON 并返回到 Java 中的 XML
- python-3.x - 根据熊猫中给定列的值保留行