【发布时间】:2020-03-05 01:12:28
【问题描述】:
这几天一直在尝试解决这个问题,现在是我寻求帮助的时候了。我知道如何使用普通的 Swift 来做到这一点,但目前 SwiftUI 对我来说还是有点不同。我有一个检查某事是否属实的功能。它通过 3 个 if 语句,如果一个为真,则它返回一个我创建为真的布尔值。一旦 3 个中的一个为真,我希望弹出某个警报。我已在正文中粘贴了所有 3 个警报:一些带有正确变量的视图,但它没有显示任何内容。如果我注释掉 2 并保留 1 Alert 未注释,那么它可以工作。所以我知道我的 if 语句是正确的。这就是我如何将它呈现给身体的方式,这就是我坚持的地方。请参阅下文,如果您有其他选择。
func showTip() {
if (stepFive <= Float(18)) {
under = true
}
else if (stepFive >= Float(18) && stepFive <= Float(18.5)) {
thin = true
}
else if (stepFive >= 18.6) && (stepFive <= 24.9) {
healthy = true
}
}
var body: some View {
ZStack {
Color.blue
.edgesIgnoringSafeArea(.all)
.alert(isPresented: $under) {
Alert(title: Text("Results"), message: Text("A of less than 18 means you are under weight. "), dismissButton: .default(Text("OK")))
}
.alert(isPresented: $thin) {
Alert(title: Text("Results"), message: Text("A of less than 18.5 indicates you are thin"), dismissButton: .default(Text("OK")))
}
.alert(isPresented: $healthy) {
Alert(title: Text("Results"), message: Text("A between 18.6 and 24.9 indicates you are at a healthy"), dismissButton: .default(Text("OK")))
}
}
【问题讨论】: