0Pricing
SwiftUI Academy · レッスン

選択中のタブのバインディング

コードでアクティブなタブを追跡し、変更します。

「選択中のタブのバインディング」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「選択中のタブのバインディング」レッスンは無料ですか?

はい。「選択中のタブのバインディング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「選択中のタブのバインディング」で何を学びますか?

コードでアクティブなタブを追跡し、変更します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「選択中のタブのバインディング」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. タブバーの作成
  2. タブ項目とバッジ
  3. 選択中のタブのバインディング
  4. タブとスタックの組み合わせ
← SwiftUI Academyに戻る