Skip to content

Instantly share code, notes, and snippets.

@tgunr
Created October 7, 2019 15:41
Show Gist options
  • Save tgunr/afe7329038427d4190c7596a297f8595 to your computer and use it in GitHub Desktop.
Save tgunr/afe7329038427d4190c7596a297f8595 to your computer and use it in GitHub Desktop.
Initialize Subview
...
@State var medication: Medication
...
TakeView(hour: $medication.interval)
struct TakeView: View {
@Binding var hour: Int
@State private var hourString: String = ""
let range = 1...12
var body: some View {
let binding = Binding<Int>(
get: { self.hour },
set: {
self.hour = $0
self.hourString = String(self.hour)
})
return Stepper(value: binding, in: range) {
if hour > 1 {
Text(verbatim: "\(hourString) hours")
} else {
Text(verbatim: "hour")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment