Selecionando opções com Picker
Ofereça opções com seletores de menu e segmentados.
Selecionando opções com Picker é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de SwiftUI Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de SwiftUI Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. 📋
Aprenda Swift com um tutor de IA — grátis
Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.
- Cursos
- 30
- Aulas
- 120
Perguntas Frequentes
A aula “Selecionando opções com Picker” é grátis?
Sim — o texto completo de “Selecionando opções com Picker” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de SwiftUI Academy, atualize para CoddyKit PRO. O curso de SwiftUI Academy inclui 4 aulas no total.
O que vou aprender em “Selecionando opções com Picker”?
Ofereça opções com seletores de menu e segmentados. Você pratica SwiftUI Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar SwiftUI Academy?
Nenhuma experiência prévia é necessária. SwiftUI Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Selecionando opções com Picker”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de SwiftUI Academy?
Sim. Cada aula de SwiftUI Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Ativado e desativado com Toggle
- Escolhendo valores com Slider
- Incrementando com Stepper
- Selecionando opções com Picker