几天来一直试图解决这个问题,现在该我寻求帮助了。我知道如何使用普通的Swift来执行此操作,但是SwiftUI目前与我有点不同。我有一个检查是否为真的函数。它经过3条if语句,如果一个为true,则返回我创建为true的布尔值。一旦这3个为真,我希望弹出一个警报。我已经将所有3个Alert粘贴到了正文中:一些具有正确变量的View,但未显示任何变量。如果我注释掉2并留下1条警报未注释,则它起作用。所以我知道我的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")))
}
}
答案 0 :(得分:1)
更新以显示具有showTip
功能的解决方案:
struct ComplexAlertWith3Variables: View {
@State var under = false
@State var thin = false
@State var healthy = false
@State var stepFive: Float = 18.3 // I don't know how it changes. From somewhere. and it doesn't matter now
private var messageText: String {
return thin ? "A of less than 18.5 indicates you are thin" : "" // compute other variants
}
var body: some View {
let needToShowAlert: Binding<Bool> = Binding<Bool>(
get: { self.under || self.thin || self.healthy },
set: { if $0 == false { self.under = $0; self.thin = $0; self.healthy = $0 } })
return Text("Fire show tip function, because I don't know from where it fires in your code")
.onTapGesture { self.showTip() } // fire your function. so you see, it doesn't matter from where you change variables
.alert(isPresented: needToShowAlert) {
Alert(title: Text("Results"), message: Text(self.messageText), dismissButton: .default(Text("OK")))
}
}
// your function (makes it shorter to fit more lines of code)
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 }
}
}
点击文本 OR 时的结果会触发其他地方的功能:
我对此进行了一些尝试,我想可以为您提出一个解决方案。关于making custom Binding
variable,并在body
中使用它。除了在第一个变体中,我还使用enum
编写了决策,但您也可以在代码段中找到包含3个变量的解决方案:
struct ComplexAlert: View {
@State private var option: SomeOption = .unselected
var body: some View {
// DISCLAIMER: I wrote in a such a way to fit more lines of code on the page
// the main here is an idea, not code style =)
let needToShowAlert: Binding<Bool> = Binding<Bool>(
get: { self.option != .unselected },
set: { if $0 == false { self.option = .unselected } })
return VStack {
Button(action: { self.option = .under }) { Text("make under") }
Button(action: { self.option = .thin }) { Text("make thin") }
Button(action: { self.option = .healthy }) { Text("make healthy") }
}
.alert(isPresented: needToShowAlert) {
Alert(title: Text("Results"), message: Text(self.option.getAlertMessage()), dismissButton: .default(Text("OK")))
}
}
}
// MARK: if you need to use 3 variables:
struct ComplexAlertWith3Variables: View {
@State var under = false
@State var thin = false
@State var healthy = false
private var messageText: String {
return under ? "under" : "" // compute other varianst
}
var body: some View {
let needToShowAlert: Binding<Bool> = Binding<Bool>(
get: { self.under || self.thin || self.healthy },
set: { if $0 == false { self.under = $0; self.thin = $0; self.healthy = $0 } })
return Text("Hello")
.onTapGesture { self.under = true }
.alert(isPresented: needToShowAlert) {
Alert(title: Text("Results"), message: Text(self.messageText), dismissButton: .default(Text("OK")))
}
}
}
// MARK: enum which was used in the first solution:
enum SomeOption {
case unselected
case under
case thin
case healthy
func getAlertMessage() -> String {
switch self {
case .unselected:
return ""
case .under:
return "A of less than 18 means you are under weight. "
case .thin:
return "A of less than 18.5 indicates you are thin"
case .healthy:
return "A between 18.6 and 24.9 indicates you are at a healthy"
}
}
}
ComplexAlert
的结果将是: