0Pricing
SwiftUI Academy · บทเรียน

การเรียกใช้โค้ดเมื่อแตะ

พิมพ์ นับ และเรียกใช้ตรรกะจากการแตะ

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

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

บทเรียน “การเรียกใช้โค้ดเมื่อแตะ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเรียกใช้โค้ดเมื่อแตะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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. การปิดใช้และเปิดใช้ปุ่ม
← กลับไปที่ SwiftUI Academy