SwiftUI Academy · บทเรียน

การผูกแท็บที่เลือก

ติดตามและเปลี่ยนแท็บที่ใช้งานอยู่ในโค้ด

บทเรียน 3 จาก 413 ขั้นตอน

การผูกแท็บที่เลือก เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. 🎉

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “การผูกแท็บที่เลือก” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การผูกแท็บที่เลือก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การผูกแท็บที่เลือก”

ติดตามและเปลี่ยนแท็บที่ใช้งานอยู่ในโค้ด คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การผูกแท็บที่เลือก” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสร้างแถบแท็บ
  2. รายการแท็บและป้ายแจ้งเตือน
  3. การผูกแท็บที่เลือก
  4. การรวมแท็บเข้ากับ Stack
← กลับไปที่ SwiftUI Academy