0Pricing
SwiftUI Academy · 课时

使用 Picker 选择选项

使用菜单式和分段式选择器提供选项。

使用 Picker 选择选项 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 SwiftUI Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 SwiftUI Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Meet the Picker

A Picker lets users choose one option from several. It is the go-to for selecting a category, color, or plan. 📋

Picker Tracks a Selection

A Picker stores the selected value in state. As the user chooses, that bound property updates to match.

@State private var fruit = "Apple"

Bind the Selection

Pass a label and the selection binding so the Picker knows which property to read and update.

Picker("Fruit", selection: $fruit) {
    // options go here
}

List the Options

Inside the closure, add a view per choice and tag each with its value using tag so selection can match it.

Text("Apple").tag("Apple")
Text("Pear").tag("Pear")

Loop with ForEach

For many options, generate them with ForEach over an array instead of writing each row by hand.

ForEach(fruits, id: \.self) { name in
    Text(name).tag(name)
}

Match Selection Types

The selection type must match the tag type. If you select a String, every tag must also be a String.

The Menu Style

Apply pickerStyle to change the look. The menu style shows the choice and pops options on tap.

Picker("Fruit", selection: $fruit) { }
    .pickerStyle(.menu)

The Segmented Style

For a few short options, the segmented style lays them out as a row of side-by-side buttons.

.pickerStyle(.segmented)

The Wheel Style

The wheel style shows a spinning drum, classic for dates or long lists where scrolling feels natural.

.pickerStyle(.wheel)

React to the Choice

Read the bound value anywhere to react to the user's pick, like showing the selected fruit in a Text.

Text("You picked \(fruit)")

The Full View

Here is a complete Picker: a selection binding, options from ForEach, and the segmented style applied.

struct ChooseView: View {
    @State private var fruit = "Apple"
    let fruits = ["Apple", "Pear"]
    var body: some View {
        Picker("Fruit", selection: $fruit) {
            ForEach(fruits, id: \.self) { Text($0).tag($0) }
        }
    }
}

Quick Check

Why must each Picker option be tagged?

Recap

A Picker binds to a selection, tags each option, and changes look with menu, segmented, or wheel styles. 📋

常见问题解答

「使用 Picker 选择选项」课时是免费的吗?

是的 — 「使用 Picker 选择选项」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。

「使用 Picker 选择选项」这节课中我会学到什么?

使用菜单式和分段式选择器提供选项。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 SwiftUI Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 Picker 选择选项」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 SwiftUI Academy 课中编写并运行代码吗?

能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 Toggle 切换开关
  2. 使用 Slider 选择数值
  3. 使用 Stepper 递增和递减
  4. 使用 Picker 选择选项
← 返回 SwiftUI Academy