init(sources:isOn:label:)
Creates a toggle representing a collection of values with a custom label.
Declaration
init<C>(sources: C, isOn: KeyPath<C.Element, Binding<Bool>>, @ViewBuilder label: () -> Label) where C : RandomAccessCollectionParameters
- sources:
A collection of values used as the source for rendering the Toggle’s state.
- isOn:
The key path of the values that determines whether the toggle is on, mixed or off.
- label:
A view that describes the purpose of the toggle.
Discussion
The following example creates a single toggle that represents the state of multiple alarms:
struct Alarm: Hashable, Identifiable {
var id = UUID()
var isOn = false
var name = ""
}
@State private var alarms = [
Alarm(isOn: true, name: "Morning"),
Alarm(isOn: false, name: "Evening")
]
Toggle(sources: $alarms, isOn: \.isOn) {
Text("Enable all alarms")
}