Ios 如何创建具有动态元素数的导航堆栈?
我试图在swiftui中创建一个标准的导航堆栈,关键是我可以拥有任意数量的页面。这是我的第一次尝试,但由于某些原因,我的选择变量在我尝试导航通过第一个列表元素后一直被重置为nullIos 如何创建具有动态元素数的导航堆栈?,ios,swift,swiftui,swiftui-navigationlink,swiftui-navigationview,Ios,Swift,Swiftui,Swiftui Navigationlink,Swiftui Navigationview,我试图在swiftui中创建一个标准的导航堆栈,关键是我可以拥有任意数量的页面。这是我的第一次尝试,但由于某些原因,我的选择变量在我尝试导航通过第一个列表元素后一直被重置为null PrimerView() ForEach(vm.sections!.indices) { index in NavigationLink(destination: formSection(vm.sections![index]), tag: index,
PrimerView()
ForEach(vm.sections!.indices) { index in
NavigationLink(destination: formSection(vm.sections![index]),
tag: index,
selection: $vm.sectionIndex) { EmptyView() }
}
NavigationLink(destination: formConfirmation, tag: vm.sectionsCount, selection: $vm.sectionIndex) { EmptyView() }
如果人们想看到目的地代码的样子:
func formSection(_ section: FormSection) -> some View {
ScrollView {
VStack {
Text("REQUIRED_HINT".localized)
.font(.subheadline)
.padding(.vertical)
ForEach(section.questions) { question in
QuestionAndAnswerView(question: question)
}
nextButton
.disabled(section.canProgress)
}
}
.navigationTitle(sectionDescription)
.navigationBarTitleDisplayMode(.inline)
.padding()
.background(Color(.systemGroupedBackground))
}
var formConfirmation: some View {
VStack {
FormConfirmationView(FormConfirmation(taskName: vm.title, points: vm.primer.points ?? 0))
nextButton
}
.padding()
.background(Color(.systemGroupedBackground))
}
下面是下一个按钮逻辑:
var nextButton: some View {
Button(action: {
if isLastIndex {
dismiss()
} else if vm.sectionIndex == nil {
vm.sectionIndex = 0
} else if let index = vm.sectionIndex {
vm.sectionIndex = index + 1
}
}) {
Text((vm.sectionIndex ?? 0) >= vm.sectionsCount ? "NAV_DONE".localized : "NAV_NEXT".localized)
.font(.title3)
.frame(maxWidth: .infinity)
.frame(height: 50)
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(10)
.padding(.bottom, 40)
}
}
所以我发现你需要给导航链接添加一个id标签。我给了它一个默认的uuid。特别是当它是在迭代中创建的。例如:
NavigationLink(destination: destination, tag: tag, selection: selection) { EmptyView() }
.id(UUID())
无法复制-你能提供一个最小的例子吗?