我在列表中的 ForEach 中使用 NavigationLink 来构建一个基本的按钮列表,每个按钮都指向一个单独的详细信息屏幕。
当我点击任何列表单元格时,它会转换到该单元格的详细信息视图,但随后会立即弹回主菜单屏幕。
不使用 ForEach 有助于避免这种行为,但不是所希望的。
这是相关的代码:
struct MainMenuView: View {
...
private let menuItems: [MainMenuItem] = [
MainMenuItem(type: .type1),
MainMenuItem(type: .type2),
MainMenuItem(type: .typeN),
]
var body: some View {
List {
ForEach(menuItems) { item in
NavigationLink(destination: self.destination(item.destination)) {
MainMenuCell(menuItem: item)
}
}
}
}
// Constructs destination views for the navigation link
private func destination(_ destination: ScreenDestination) -> AnyView {
switch destination {
case .type1:
return factory.makeType1Screen()
case .type2:
return factory.makeType2Screen()
case .typeN:
return factory.makeTypeNScreen()
} …Run Code Online (Sandbox Code Playgroud)