SwiftUI Academy · บทเรียน

การสร้างปุ่มที่แตะได้

สร้าง Button พร้อมป้ายกำกับและการกระทำ

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

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

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

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

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

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

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

บทเรียน “การสร้างปุ่มที่แตะได้” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การสร้างปุ่มที่แตะได้”

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

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

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

บทเรียน “การสร้างปุ่มที่แตะได้” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การสร้างปุ่มที่แตะได้
  2. การตกแต่งปุ่ม
  3. การเรียกใช้โค้ดเมื่อแตะ
  4. การปิดใช้และเปิดใช้ปุ่ม
← กลับไปที่ SwiftUI Academy