I'm having a difficult time running a couple functions on a .sheet when a button is selected. Pretty much, when I select the View Results button and the sheet opens, both functions are running as if they are not of true value, however I do print the values of these Boolean variables as the button is hit and they they print out the correct values.
I have two major toggles here. One called Disabled & the other MedicareBen along with their State's:
@State var disabled: Bool = false
@State private var medicareBen: Bool = false
Section("Does anyone have:") {
Toggle(isOn: self.$medicareBen) { Text("Medicare")}
.tint(.blue)
Toggle(isOn: self.$disabled) { Text("A Disablility")}
.tint(.blue)
}
I have two functions that determine the Booleans final value: The first function below, mspEligibility()
below primarily involves the MedicareBen toggle above, while the second function, ssdiEligibility()
involves the disabled
button above.
@State var elig: Bool = false
func mspEligibility() {
if hhmembers == 1 && age >= 17 && income <= 1449 && incomefreq == .Monthly && mspBen == false && medicareBen == true && marital == .Single || marital == .Divorce || marital == .Widow {
print("MSP Eligible 1")
elig = true
} else if hhmembers == 1 && age >= 17 && income <= 17388 && incomefreq == .Yearly && mspBen == false && medicareBen == true && marital == .Single || marital == .Divorce || marital == .Widow {
print("MSP Eligible 1")
elig = true
} else if medicareBen == false {
print("Not MSP Eligible")
elig = false
} else if ssiBen == true || medicaidBen == true || mspBen == true || marital == .Married {
print("Not MSP Eligible")
elig = false
} else if hhmembers >= 2 {
print("Not MSP Eligible")
elig = false
} else {
print("Not MSP Eligible")
elig = false
}
}
The function that involves the Disabled toggle:
@State var ssdiElig: Bool = false
func ssdiEligibility() {
if disabled == true && income <= 803 {
print("SSDI Eligible")
ssdiElig = true
disabled = true
} else if ssdiBen == true || ssiBen == true {
print("Not SSDI Eligible")
ssdiElig = false
disabled = false
} else {
print("Not SSDI Eligible")
ssdiElig = false
disabled = false
}
}
Below is the button, which carries a bunch of different Eligibility functions that aren't really involved here. However, the button also presents a .sheet
has 2 conditional statements that are a bit flaky. As I mentioned you'll see I print a few different things in the console, print(elig)
, print(ssdiElig)
, print(disabled)
, the Boolean values end up coming up as expected based on the prior functions I pasted above but it seems I have to toggle the disability button or Medicare button a couple times before the if conditions work properly. Is there a way around this? Or, maybe somehow binding them to another View the sheet opens? any thoughts?
ZStack {
VStack {
Button(action: {mspEligibility(); ssdiEligibility(); ssiEligibility(); mspEligibility2(); PAzipcode(); mspEligibility3();
self.isOpen = true
print(elig)
print(ssdiElig)
print(disabled)
}, label: {
Text("View Results")
}).sheet(isPresented: $isOpen, content: {
if ssdiElig {
ZStack {
RoundedRectangle(cornerRadius: 25)
.fill(Color.white)
.frame(width: 375, height: 125)
.shadow(color: Color.black.opacity(0.5), radius: 10, x: 10, y: 10)
.shadow(color: Color.white.opacity(0.7), radius: 10, x: -5, y: -5)
HStack {
Text("Eligible for SSDI")
.font(.headline)
.padding(60)
Image(systemName: "face.dashed")
.resizable()
.frame(width: 60, height: 60)
.padding(7)
}
}
} else {
Text("No")
}
if elig == true {
Text("Eligible for MSP")
ZStack {
RoundedRectangle(cornerRadius: 25)
.fill(Color.white)
.frame(width: 375, height: 125)
.shadow(color: Color.black.opacity(0.5), radius: 10, x: 10, y: 10)
.shadow(color: Color.white.opacity(0.7), radius: 10, x: -5, y: -5)
HStack {
Text("Eligible for MSP")
.font(.headline)
.padding(60)
Image(systemName: "face.dashed")
.resizable()
.frame(width: 60, height: 60)
.padding(7)
}
}
} else {
Text("No")
}
}
)}
}