简体   繁体   English

SwiftUI - 导航链接多次打开同一个视图

[英]SwiftUI - Navigation Link opening the same view multiple times

I have a list in ViewA which has 3 rows.我在 ViewA 中有一个包含 3 行的列表。 ViewA is a grand-child of the main view(not sure if this info matters, but just providing since I don't know where the actual bug is. Here is how the stack is ContentView -> opens Main Menu View(MMV) -> select an item from MMV and it opens -> Settings View -> select an item from Settings and it opens ViewA). ViewA 是主视图的孙子(不确定此信息是否重要,但只是提供,因为我不知道实际的错误在哪里。这是堆栈的内容视图 -> 打开主菜单视图(MMV) - > 从 MMV 中选择一个项目并打开 -> 设置视图 -> 从设置中选择一个项目并打开 ViewA)。

When I click on a row in ViewA, another view should opens up let's say ViewB.当我单击 ViewA 中的一行时,应该会打开另一个视图,比如说 ViewB。 However I am seeing ViewB instantiating 3 times ie I see ViewB appearing 3 times on the screen and then it stops at the last instance.但是我看到 ViewB 实例化 3 次,即我看到 ViewB 在屏幕上出现 3 次,然后它在最后一个实例停止。

I am not sure why this is happening and what the actual bug is.我不确定为什么会发生这种情况以及实际的错误是什么。 Here is my code:这是我的代码:

ViewA(MenuViewOptions):视图A(菜单视图选项):

struct MenuViewOptions: View {
    @State var destination: MenuViewType?
    var isActive: Binding<Bool> { Binding(get: { destination != nil }, set: { _ in destination = nil } ) }
    
    var body: some View {
        VStack {
            List(SettingsOptions().menuViewSettingsOptions) { item in
                NavigationLink(isActive: isActive, destination: {destination?.view} ) {
                    MenuViewOptionRowView(menuViewItem: item, destination: $destination)
                }
            }
        }.navigationBarTitle("Settings")
    }
}

MenuViewType code:菜单视图类型代码:

enum MenuViewType: Int, CaseIterable, Hashable {
    case circularGrid = 0, regularList = 1, capsuleGrid = 2
    
    @ViewBuilder var view: some View {
        switch self {
            case .circularGrid: MainMenuCircularGridViewNew()
            case .regularList: MainMenuListView()
            case .capsuleGrid: MainMenuCapsuleGridView()
        }
    }
}

SettingsOptions().menuViewSettingsOptions code: SettingsOptions().menuViewSettingsOptions 代码:

struct SettingsOptions {
    // View inside menu view settings
    let menuViewSettingsOptions: [MenuViewSettingsItem] = [
        MenuViewSettingsItem(id: 0, name: "Circular Grid", imageName: "circle.grid.2x2", isSelected: false),
        MenuViewSettingsItem(id: 1, name: "List", imageName: "list.dash", isSelected: false),
        MenuViewSettingsItem(id: 2, name: "Capsule Grid", imageName: "rectangle.grid.2x2", isSelected: false),
    ]
}

MenuViewOptionRowView code MenuViewOptionRow查看代码

struct MenuViewOptionRowView: View {
    
    let menuViewItem: MenuViewSettingsItem
    @ObservedObject var userSettingsStore = UserSettingsStore()
    @Binding var destination: MenuViewType?

    var body: some View {
        HStack {
            Image(systemName: menuViewItem.imageName)
                .circularImageStyle(width: 15, height: 15, padding: 5, backgroundColor: Color(red: 34 / 255, green: 34 / 255, blue: 34 / 255), foregroundColor: Color(red: 23 / 255, green: 121 / 255, blue: 232 / 255))
            Text(menuViewItem.name)
            Spacer()
            if menuViewItem.id == userSettingsStore.menuViewSelection {
                Image(systemName: "checkmark").foregroundColor(Color(red: 23 / 255, green: 121 / 255, blue: 232 / 255))
            }
        }.onTapGesture {
            userSettingsStore.menuViewSelection = self.menuViewItem.id
            destination = MenuViewType(rawValue: self.menuViewItem.id)
        }
    }
}

Basically I am setting the destination in MenuViewOptionRowView and once it's set, isActive in MenuViewOptions becomes true and NavigationLink gets fired.基本上我在 MenuViewOptionRowView 中设置目标,一旦设置,MenuViewOptions 中的 isActive 变为 true 并且 NavigationLink 被触发。 I am seeing the row navigates to the correct view, but just that the view instantiates 3 times which I am not sure if it is because I have 3 rows in the list.我看到该行导航到正确的视图,但只是视图实例化了 3 次,我不确定是否是因为列表中有 3 行。

If you have any idea of what could be the issue, please do let me know.如果您对可能是什么问题有任何想法,请告诉我。 New to SwiftUI. SwiftUI 的新手。

Thanks!谢谢!

You set destination once, but with this you activate all links in List, because they are all bound to one isActive .您设置了一次目的地,但这样您就激活了 List 中的所有链接,因为它们都绑定到一个isActive

With such code design I would propose the following solution: one destination - one link, destination is already set on tap, so... (code in question is not testable so just as-is)有了这样的代码设计,我会提出以下解决方案:一个目的地 - 一个链接,目的地已经设置好,所以......(有问题的代码不可测试,所以原样)

List(SettingsOptions().menuViewSettingsOptions) { item in
    MenuViewOptionRowView(menuViewItem: item, destination: $destination)
}
.background(
    NavigationLink(isActive: isActive, destination: {destination?.view} ) {
        EmptyView()
    }
)

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

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