SwiftUI Infinite 循环在选取器中



尝试做一个功能,但在返回"一些视图"时遇到问题。我也在尝试使用继续语句进行 while 循环,但这在 viewBuilder 的正文中是不允许的......当条件发生变化时,使用选取器的最简单方法是什么?

var body: some View {
VStack{
Group{
Form {
Section(header: Text("(screeningTable())")) {
Picker(selection: $updateMaleBodyCompView.age, label: Text("Select age")) {
List(maleDataModel.ageArray, id: .self) { i in
Text("(i, specifier: "%g")-years-old")
}
}
if updateMaleBodyCompView.age == 0 || updateMaleBodyCompView.height == 0 || updateMaleBodyCompView.weight == 0 {
Section {
Text("Fill all required fields").foregroundColor(.red)
}
}
if screeningTable() == "No Go" {
Section(header: Text("Tape Mesurements")) {
MaleTapeView()
}
}
if updateMaleBodyCompView.age != 0 && updateMaleBodyCompView.height != 0 && updateMaleBodyCompView.weight != 0 && screeningTable() != "No Go"{
Section(header: Text("You are not required to tape").foregroundColor(.blue)) {
MaleSaveButton()
}
}
}
}

我不确定您的代码是否有效,但是如果您想要if语句,可以做的是将它们括在ZStacks

var body: some View {
VStack{
Group{
Form {
Section(header: Text("(screeningTable())")) {
Picker(selection: $updateMaleBodyCompView.age, label: Text("Select age")) {
List(maleDataModel.ageArray, id: .self) { i in
Text("(i, specifier: "%g")-years-old")
}
}
ZStack {
if updateMaleBodyCompView.age == 0 || updateMaleBodyCompView.height == 0 || updateMaleBodyCompView.weight == 0 {
Section {
Text("Fill all required fields").foregroundColor(.red)
}
}
}
ZStack {
if screeningTable() == "No Go" {
Section(header: Text("Tape Mesurements")) {
MaleTapeView()
}
}
}
ZStack {
if updateMaleBodyCompView.age != 0 && updateMaleBodyCompView.height != 0 && updateMaleBodyCompView.weight != 0 && screeningTable() != "No Go"{
Section(header: Text("You are not required to tape").foregroundColor(.blue)) {
MaleSaveButton()
}
}
}
}
}

我从设计+代码课程中得到了这个。我可以看到 @State 属性如何在没有无休止的条件语句的情况下来回切换......

BottomCardView(showRing: $showCard)
.offset(y: showCard ? screen.height/2 - 50 : screen.height)
.offset(y: bottomState.height)
.blur(radius: show ? 20 : 0)
.animation(.spring(response: 0.5, dampingFraction: 0.7, blendDuration: 0))
.gesture(
DragGesture()
.onChanged { value in
self.bottomState = value.translation
if self.showFull {
self.bottomState.height += -300
}
if self.bottomState.height < -300 {
self.bottomState.height = -300
}
}
.onEnded { value in
if self.bottomState.height > 50 {
self.showCard = false
}
if (self.bottomState.height < -100 && !self.showFull) || (self.bottomState.height < -250 && self.showFull) {
self.bottomState.height = -300
self.showFull = true
} else {
self.bottomState = .zero
self.showFull = false
}
}
)

最新更新