0Pricing
SwiftUI Academy · Aula

Vinculação da aba selecionada

Acompanhe e altere a aba ativa no código.

Vinculação da aba selecionada é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 3 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.

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 = 0

Bind 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 = 1

A 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. 🎉

Perguntas Frequentes

A aula “Vinculação da aba selecionada” é grátis?

Sim — o texto completo de “Vinculação da aba selecionada” é 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 “Vinculação da aba selecionada”?

Acompanhe e altere a aba ativa no código. 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 3 de 4.

Quanto tempo leva a aula “Vinculação da aba selecionada”?

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

  1. Criando uma barra de abas
  2. Itens de aba e distintivos
  3. Vinculação da aba selecionada
  4. Combinando abas com pilhas
← Voltar para SwiftUI Academy