【发布时间】:2021-07-04 15:04:50
【问题描述】:
我一直在尝试在我的 SwiftUI 代码中实现编程导航,但收效甚微。我打算为以下代码做的是检查用户是否填写了屏幕上的每个文本字段,如果是,则进入下一个屏幕。如果没有,则显示警报并允许用户返回并备份文本字段。然而,有一个障碍——如果用户在第一次尝试时完全填写了表单,他们可能会继续,但如果没有,他们会收到警报(如预期的那样),但是一旦他们再次填写表单完成,点击 NavigationLink 没有任何作用,而它应该允许用户转到下一个屏幕。
// this part of the code defines the validation logic
@State private var textFieldsInvalid = false
@State private var lineupIsReady = false
/// this is the navigation link i'm trying to fix
NavigationLink(destination: SetHomeLineup(), isActive: self.$lineupIsReady) {
HStack {
Spacer()
Label("Submit", systemImage: "chevron.right.circle.fill")
Spacer()
}
.padding(.all, 18)
.background(Color.green)
.foregroundColor(.white)
.font(.title)
.onTapGesture {
for name in checkIfFieldsValid {
if !(name.isEmpty) {
self.lineupIsReady = true
}
else {
self.textFieldsInvalid = true
}
}
}
}
/* this is the alert modifier attached to a VStack that contains a Form
(with its text fields) and the navigation link above. I haven't included the Form
because it's too large and not the problem area for now. */
}
.edgesIgnoringSafeArea(.bottom)
.alert(isPresented: $textFieldsInvalid, content: {
Alert(title: Text("Incomplete Information"), message: Text("Please make sure that you have filled in all fields."), dismissButton: .default(Text("Back")))
就上下文而言,checkIfFieldsValid 是一个存储九名玩家姓名的数组(由用户输入文本字段并存储在@State private var 中)。我可以包含这些代码以及必要时的表单,但我相当确定它们不会参与错误,因为代码能够很好地读取和访问用户输入的名称。
谁能指出我的代码中导致错误的区域?我是初学者,只有两周的代码经验,所以任何帮助将不胜感激:)
【问题讨论】:
-
我认为我有逻辑上的错误 -> 当用户未能填写文本字段并按下 NavLink 时,lineupIsReady 的状态变为 True。如果用户纠正了所有错误,再次填写并按下 lineupIsReady,那么显然不会发生任何事情,因为 True 状态保持不变。因此,当您在失败后向用户发出警报时,您应该在警报按钮中实现 lineupIsReady = false 。我希望这会有所帮助。
-
是的,就是这样。谢谢!