0Pricing
SwiftUI Academy · درس

إنشاء زر قابل للنقر

أنشئ Button مع تسمية وإجراء

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

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Buttons Matter

Most apps come alive when you tap something. A Button is the simplest way to let people trigger an action in SwiftUI. 👆

The Button View

A Button is just a view, like Text or Image. You place it in your body and SwiftUI draws a tappable control for you.

Button("Tap me") {
    print("Tapped!")
}

Two Parts of a Button

Every button has two parts: a label that you see, and an action that runs when you tap it. Both are required.

The Simple Title Form

The quickest button takes a title string first and the action closure second. SwiftUI turns the string into a Text label for you.

Button("Save") {
    saveDocument()
}

The Action Closure

The braces hold the action closure. Whatever code you write inside runs once each time the button is tapped, and not before.

Button("Greet") {
    print("Hello there")
}

A Custom Label

Need more than text? Use the label form: pass an action, then a closure that returns any view you like for the look.

Button {
    likePost()
} label: {
    Image(systemName: "heart")
}

Labels Can Be Rich

Because the label is a view, you can combine an icon and text together inside a stack for a friendlier, clearer tappable area.

Button {
    addItem()
} label: {
    Label("Add", systemImage: "plus")
}

Buttons Live in the Body

A button goes inside your view's body, just like any other view. SwiftUI handles drawing it and listening for taps automatically.

var body: some View {
    Button("Continue") {
        goNext()
    }
}

The Tap Target

The whole label area is tappable, so a bigger label is easier to hit. Apple suggests a comfortable tap target of about 44 points.

Calling a Function on Tap

Keep the closure tidy by calling a function instead of cramming logic inline. The action just points to the work you defined elsewhere.

Button("Refresh") {
    reloadData()
}

No Tap, No Action

The action does not run when the screen loads. It waits patiently and only fires on tap, which keeps your view predictable.

Quick Check

Let's make sure the button basics clicked.

Recap

Nice work! A Button pairs a label you see with an action that runs on tap. Use the simple title form or a custom label view. ✨

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

هل درس «إنشاء زر قابل للنقر» مجاني؟

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

ماذا ستتعلم في «إنشاء زر قابل للنقر»؟

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

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

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

كم من الوقت يستغرق درس «إنشاء زر قابل للنقر»؟

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

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

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

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

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