Vinculación de la pestaña seleccionada
Siga y cambie la pestaña activa mediante código.
Vinculación de la pestaña seleccionada es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de SwiftUI Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de SwiftUI Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Knowing the Active Tab
Sometimes your code needs to know or change which tab is showing. For that you bind the selection to your own state. 🎯
Give Each Tab a Tag
First mark each tab with a unique tag value so SwiftUI can identify which one is selected.
HomeScreen()
.tabItem { Label("Home", systemImage: "house") }
.tag(0)Store the Selection
Add a @State property to hold the current tab tag. Its value tells you which tab is active.
@State private var selectedTab = 0Bind selection to TabView
Pass that state to the TabView selection parameter using a dollar sign to create a two-way binding.
TabView(selection: $selectedTab) {
// tabs with .tag(...)
}Tags Must Match
The tag type and the selection type must match. If tags are Int, selectedTab must be an Int too.
Reading Which Tab Is Open
Once bound, selectedTab updates whenever the user taps. You can read it anywhere to react to the current tab.
Switching Tabs in Code
Change the state value and the UI follows. Setting selectedTab jumps the user to that tab programmatically.
selectedTab = 1A Jump-to-Tab Button
A button can switch tabs by writing to the binding, handy for a "Go to Settings" shortcut deep in your app.
Button("Open Settings") {
selectedTab = 1
}Enums Make Tags Safer
Plain numbers are easy to mix up. A enum of named cases makes each tag readable and mistake-proof.
enum Tab { case home, settings }Re-Tapping the Selected Tab
Tapping the active tab again keeps the same selection. You can watch for this to scroll a list back to the top.
When You Need the Binding
Skip the binding if tabs are fully independent. Add it only when code must read or set the active tab.
Quick Check
Let us check how SwiftUI tracks the active tab.
Recap: Selected Tab Binding
Tag each tab and bind a @State value to selection, and you can both read the active tab and switch tabs from code. 🎉
Preguntas frecuentes
¿La lección «Vinculación de la pestaña seleccionada» es gratis?
Sí — el texto completo de «Vinculación de la pestaña seleccionada» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de SwiftUI Academy, actualiza a CoddyKit PRO. El curso de SwiftUI Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Vinculación de la pestaña seleccionada»?
Siga y cambie la pestaña activa mediante código. Practicas SwiftUI Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar SwiftUI Academy?
No se requiere experiencia previa. SwiftUI Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «Vinculación de la pestaña seleccionada»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de SwiftUI Academy?
Sí. Cada lección de SwiftUI Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Creación de una barra de pestañas
- Elementos de pestaña e insignias
- Vinculación de la pestaña seleccionada
- Combinación de pestañas con pilas