Einen antippbaren Button erstellen
Erstellen Sie einen Button mit einem Label und einer Aktion.
Einen antippbaren Button erstellen ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. ✨
Häufig gestellte Fragen
Ist die Lektion „Einen antippbaren Button erstellen“ kostenlos?
Ja — der vollständige Text von „Einen antippbaren Button erstellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Einen antippbaren Button erstellen“?
Erstellen Sie einen Button mit einem Label und einer Aktion. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um SwiftUI Academy zu starten?
Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Einen antippbaren Button erstellen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?
Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einen antippbaren Button erstellen
- Buttons gestalten
- Code beim Antippen ausführen
- Buttons deaktivieren und aktivieren