I'd like to call a function within a switch case
switch drgpick {
case "Remifentanilo 5 mg en 250 ml" :
remi5en250()
}
but it seems I'm obliged to use a button to call the function like so
switch drgpick {
case "Remifentanilo 5 mg en 250 ml" :
Button ("\(drgpick)") {
remi5en250()
}
is there a way to call the function without having to use a button?
You can call a func in a modifier: .onAppear, .onChange for instance.
So, if the switch executes some code (but do not return View) Create a func
func callAction(for drgpick: String) {
switch drgpick {
case "Remifentanilo 5 mg en 250 ml" : remi5en250()
case "Remifentanilo 2.5 mg en 250 ml" : remi25en250()
case "Noradrenalina 4 mg en 250 ml" : nora4en250()
case "Noradrenalina 8 mg en 250 ml" : nora8en250()
case "Noradrenalina 20 mg en 250 ml" : nora20en250()
case "Dexmedetomidina 200 mcg en 100 ml" : dex200en100()
case "Dexmedetomidina 200 mcg en 50 ml" : dex200en50()
case "Milrinona 20 mg en 250 ml" : milri20en250()
case "Adrenalina 4 mg en 250 ml" : adre4en250()
default : break // Not a View here
}
}
Then call:
Picker("Drogas", selection: $drgpick, content: {
ForEach(opcdrg, id: \.self, content: { opcdrg in Text(opcdrg) } )
}
)
.onChange(of: drgpick) {_ in callAction(for: drgpick) }