带有各种视图目的地的导航链接在一个foreach循环中
我正在尝试在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是您在
navigationLink 内使用
按钮
。NavigationLink
已经是按钮
,因此可能存在一些问题,因为button
本身没有任何操作。因此,以下代码可能有效:The issue may be that you use
Button
insideNavigationLink
.NavigationLink
already is aButton
so there may be some issues with that as there’s no action for theButton
itself. So following code may work: