Return Value 戻り値
A view with the specified tag set. 指定されたタグを設定したあるビュー。
Availability 有効性
Technology
A view with the specified tag set. 指定されたタグを設定したあるビュー。
tag
Use this modifier to differentiate among certain selectable views, like the possible values of a Picker
or the tabs of a Tab
. Tag values can be of any type that conforms to the Hashable
protocol.
In the example below, the For
loop in the Picker
view builder iterates over the Flavor
enumeration.
下の例において、For
ループはPicker
ビュービルダーの中で、Flavor
列挙のすべてにわたって反復します。
It extracts the string value of each enumeration element for use in constructing the row label, and uses the enumeration value, cast as an optional, as input to the tag(_:)
modifier. The Picker
requires the tags to have a type that exactly matches the selection type, which in this case is an optional Flavor
.
struct FlavorPicker: View {
enum Flavor: String, CaseIterable, Identifiable {
case chocolate, vanilla, strawberry
var id: Self { self }
}
private var selectedFlavor: Flavor? = nil
var body: some View {
Picker("Flavor", selection: $selectedFlavor) {
ForEach(Flavor.allCases) { flavor in
Text(flavor.rawValue).tag(Optional(flavor))
}
}
}
}
If you change selected
to be non-optional, you need to remove the Optional
cast from the tag input to match.
A For
automatically applies a default tag to each enumerated view using the id
parameter of the corresponding element. If the element’s id
parameter and the picker’s selection
input have exactly the same type, you can omit the explicit tag modifier. To see examples that don’t require an explicit tag, see Picker
.