带有各种视图目的地的导航链接在一个foreach循环中

发布于 2025-02-08 15:45:05 字数 2580 浏览 3 评论 0原文

我正在尝试在我的Mainview中进行foreach循环,该循环将根据用户想要去的位置包含导航链接到不同视图。 我创建了枚举以获取特定视图的名称:

enum NavigationViewsNames: String, CaseIterable {
        case orders = "Orders"
        case returns = "Returns"
        case personalInfo = "Personal Info"
        case paymentDetails = "Payment Details"
        case help = "Help"
        
        static var allCases: [NavigationViewsNames] {
            return [
                .orders,
                .returns,
                .personalInfo,
                .paymentDetails,
                .help
            ]
        }
    }

并且我在视图和foreach中使用switch-case创建特定的navigation links:

VStack(alignment: .center) {
                            ForEach(NavigationViewsNames.allCases, id: \.self) { navigationViewName in
                                HStack {
                                    Spacer()
                                    NavigationLink() {
                                        switch navigationViewName {
                                        case .orders:
                                            OrdersView()
                                        case .returns:
                                            ReturnsView()
                                        case .personalInfo:
                                            PersonalInfoView()
                                        case .paymentDetails:
                                            PaymentDetailsView()
                                        case .help:
                                            HelpView()
                                        }
                                    } label: {
                                        Button(navigationViewName.rawValue) {
                                        }
                                        .buttonStyle(CustomButton(buttonColor: .white, textColor: .accentColor, onlyStroke: true, strokeColor: .accentColor, rightChevronNavigationImage: true))
                                        .frame(width: UIScreen.main.bounds.width * 0.9)
                                        .contentShape(Rectangle())
                                        .padding(.bottom, 20)
                                    }
                                    
                                    Spacer()
                                }
                            }
                        }
                    }

它导致INS IN:

所有导航链接按钮可单击,但是其中一些不会导致任何视图一次,在另一个时间他们会的。

I'm trying to make ForEach loop in my MainView which would contain NavigationLinks to different Views depending on where user wants to go.
I created enum in order to get names for specific views:

enum NavigationViewsNames: String, CaseIterable {
        case orders = "Orders"
        case returns = "Returns"
        case personalInfo = "Personal Info"
        case paymentDetails = "Payment Details"
        case help = "Help"
        
        static var allCases: [NavigationViewsNames] {
            return [
                .orders,
                .returns,
                .personalInfo,
                .paymentDetails,
                .help
            ]
        }
    }

And I use switch-case within the view and ForEach to create specific NavigationLinks:

VStack(alignment: .center) {
                            ForEach(NavigationViewsNames.allCases, id: \.self) { navigationViewName in
                                HStack {
                                    Spacer()
                                    NavigationLink() {
                                        switch navigationViewName {
                                        case .orders:
                                            OrdersView()
                                        case .returns:
                                            ReturnsView()
                                        case .personalInfo:
                                            PersonalInfoView()
                                        case .paymentDetails:
                                            PaymentDetailsView()
                                        case .help:
                                            HelpView()
                                        }
                                    } label: {
                                        Button(navigationViewName.rawValue) {
                                        }
                                        .buttonStyle(CustomButton(buttonColor: .white, textColor: .accentColor, onlyStroke: true, strokeColor: .accentColor, rightChevronNavigationImage: true))
                                        .frame(width: UIScreen.main.bounds.width * 0.9)
                                        .contentShape(Rectangle())
                                        .padding(.bottom, 20)
                                    }
                                    
                                    Spacer()
                                }
                            }
                        }
                    }

It results in:

With all NavigationLinks buttons being clickable however randomly some of them won't lead to any view at one time and at another time they will.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-02-15 15:45:05

问题可能是您在 navigationLink 内使用按钮NavigationLink已经是按钮,因此可能存在一些问题,因为button本身没有任何操作。因此,以下代码可能有效:


VStack(alignment: .center) {
                            ForEach(NavigationViewsNames.allCases, id: \.self) { navigationViewName in
                                HStack {
                                    Spacer()
                                    NavigationLink() {
                                        switch navigationViewName {
                                        case .orders:
                                            OrdersView()
                                        case .returns:
                                            ReturnsView()
                                        case .personalInfo:
                                            PersonalInfoView()
                                        case .paymentDetails:
                                            PaymentDetailsView()
                                        case .help:
                                            HelpView()
                                        }
                                    } label: {
                                        Text(navigationViewName.rawValue)
                                       
                                    }
                                     .buttonStyle(CustomButton(buttonColor: .white, textColor: .accentColor, onlyStroke: true, strokeColor: .accentColor, rightChevronNavigationImage: true))
                                     .frame(width: UIScreen.main.bounds.width * 0.9)
                                     .contentShape(Rectangle())
                                     .padding(.bottom, 20)

                                    Spacer()
                                }
                            }
                        }
                    }
    ```

The issue may be that you use Button inside NavigationLink. NavigationLink already is a Button so there may be some issues with that as there’s no action for the Button itself. So following code may work:


VStack(alignment: .center) {
                            ForEach(NavigationViewsNames.allCases, id: \.self) { navigationViewName in
                                HStack {
                                    Spacer()
                                    NavigationLink() {
                                        switch navigationViewName {
                                        case .orders:
                                            OrdersView()
                                        case .returns:
                                            ReturnsView()
                                        case .personalInfo:
                                            PersonalInfoView()
                                        case .paymentDetails:
                                            PaymentDetailsView()
                                        case .help:
                                            HelpView()
                                        }
                                    } label: {
                                        Text(navigationViewName.rawValue)
                                       
                                    }
                                     .buttonStyle(CustomButton(buttonColor: .white, textColor: .accentColor, onlyStroke: true, strokeColor: .accentColor, rightChevronNavigationImage: true))
                                     .frame(width: UIScreen.main.bounds.width * 0.9)
                                     .contentShape(Rectangle())
                                     .padding(.bottom, 20)

                                    Spacer()
                                }
                            }
                        }
                    }
    ```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文