0Pricing
SwiftUI Academy · درس

ربط علامة التبويب المحددة

تتبّع علامة التبويب النشطة وغيّرها في التعليمات البرمجية

ربط علامة التبويب المحددة درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. 🎉

الأسئلة الشائعة

هل درس «ربط علامة التبويب المحددة» مجاني؟

نعم — نص درس «ربط علامة التبويب المحددة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.

ماذا ستتعلم في «ربط علامة التبويب المحددة»؟

تتبّع علامة التبويب النشطة وغيّرها في التعليمات البرمجية تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟

لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «ربط علامة التبويب المحددة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟

نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. إنشاء شريط علامات تبويب
  2. عناصر علامات التبويب والشارات
  3. ربط علامة التبويب المحددة
  4. دمج علامات التبويب مع حاويات التكديس
← العودة إلى SwiftUI Academy