0Pricing
SwiftUI Academy · درس

تشغيل التعليمات البرمجية عند النقر

اطبع البيانات واحسبها وشغّل المنطق استجابةً للنقر

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

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

The Action Is Yours

The closure inside a button is where your app does real work. This is where you run logic, update data, or kick off a task on tap. ⚡

Printing for a Quick Test

The simplest action is a print. It writes to Xcode's console so you can confirm a tap was registered while you build.

Button("Test") {
    print("Button was tapped")
}

Changing a Value

Most actions change some data. Here we bump a count each tap. Soon you will store that value in state so the UI can react.

Button("Add one") {
    count += 1
}

Calling Your Own Function

Keep buttons readable by moving logic into a function. The action becomes a single clear line that names what happens.

Button("Submit") {
    submitForm()
}

Multiple Steps in One Tap

An action can run several lines in order. SwiftUI runs them top to bottom, so a single tap can do a small sequence of work.

Button("Reset") {
    score = 0
    message = "Cleared"
}

Conditional Logic on Tap

You can branch inside the action with an if. The tap decides what to do based on the current values of your data.

Button("Toggle") {
    if isOn { turnOff() } else { turnOn() }
}

Reading and Updating Together

A common pattern reads a value, computes something, then stores it back. The tap captures the user's intent in one place.

Button("Double it") {
    total = total * 2
}

Starting Async Work

For slow work like a network call, wrap it in a Task so the tap stays responsive while the work runs in the background.

Button("Load") {
    Task { await fetchData() }
}

Keep the Action Light

Buttons feel snappy when actions stay short. Hand heavy work to a function or Task instead of writing long logic inline.

One Tap, One Run

The action runs once per tap. If you need repeats, you control that in your logic. SwiftUI never fires the action twice for one tap.

Debugging a Silent Button

If a tap seems to do nothing, drop a print at the top of the action. Seeing it in the console confirms the button is wired correctly.

Button("Why?") {
    print("I ran")
    doWork()
}

Quick Check

Let's check how tap actions behave.

Recap

Well done! A tap can print, change values, branch with if, or start a Task. Keep actions light and call functions for bigger jobs. ⚡

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

هل درس «تشغيل التعليمات البرمجية عند النقر» مجاني؟

نعم — نص درس «تشغيل التعليمات البرمجية عند النقر» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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