Selected Tab Binding
Track and change the active tab in code.
Selected Tab Binding is a free SwiftUI Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎉
Frequently asked questions
Is the “Selected Tab Binding” lesson free?
Yes — the full text of “Selected Tab Binding” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Selected Tab Binding”?
Track and change the active tab in code. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Selected Tab Binding” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building a Tab Bar
- Tab Items & Badges
- Selected Tab Binding
- Combining Tabs with Stacks