การปิดใช้และเปิดใช้ปุ่ม
ควบคุมการโต้ตอบด้วยตัวปรับแต่ง disabled
การปิดใช้และเปิดใช้ปุ่ม เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Sometimes Tapping Should Wait
Not every button should always be tappable. A submit button might wait until a form is valid. SwiftUI handles this with the disabled modifier. 🚦
The disabled Modifier
Add disabled to any control and pass true or false. When true, the button stops responding to taps right away.
Button("Send") { send() }
.disabled(true)Driving It With a Condition
Instead of a fixed value, pass a condition. The button enables and disables itself as the underlying data changes.
Button("Send") { send() }
.disabled(message.isEmpty)Tied to State
Pair disabled with a state value and SwiftUI re-evaluates it on every change. The button reacts the moment your data updates.
@State private var name = ""
Button("Save") { save() }
.disabled(name.isEmpty)The Dimmed Look
A disabled button automatically appears dimmed. This visual cue tells people it is not ready yet, with no extra styling from you.
Validating a Form
A classic use is form validation: keep the action button off until every required field passes its check.
Button("Register") { register() }
.disabled(!isFormValid)Preventing Double Taps
During a slow task, disable the button so users cannot tap it again. Flip a loading flag while the work is running.
Button("Upload") { startUpload() }
.disabled(isLoading)Disabling a Whole Group
Apply disabled to a container and every control inside inherits it. One modifier can lock down an entire section at once.
VStack {
Button("A") { a() }
Button("B") { b() }
}
.disabled(isLocked)Children Cannot Re-enable
Once a parent is disabled, a child cannot turn itself back on. The most restrictive disabled value in the chain wins.
Disabled Still Communicates
A disabled button is not broken; it is honest. It signals that an action is unavailable right now, guiding users toward what to do first.
Enable by Flipping the Value
To re-enable, simply make the condition false again. Update the state it depends on and SwiftUI brings the button back to life.
Button("Confirm") { confirm() }
.disabled(!agreedToTerms)Quick Check
One last check on enabling and disabling.
Recap
Excellent! The disabled modifier locks a button based on a condition, dims it automatically, and re-enables when the value flips back. 🚦
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “การปิดใช้และเปิดใช้ปุ่ม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การปิดใช้และเปิดใช้ปุ่ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การปิดใช้และเปิดใช้ปุ่ม”
ควบคุมการโต้ตอบด้วยตัวปรับแต่ง disabled คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การปิดใช้และเปิดใช้ปุ่ม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างปุ่มที่แตะได้
- การตกแต่งปุ่ม
- การเรียกใช้โค้ดเมื่อแตะ
- การปิดใช้และเปิดใช้ปุ่ม