タップできる Button の作成
ラベルとアクションを備えた Button を作成します。
「タップできる Button の作成」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. ✨
よくある質問
「タップできる Button の作成」レッスンは無料ですか?
はい。「タップできる Button の作成」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「タップできる Button の作成」で何を学びますか?
ラベルとアクションを備えた Button を作成します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「タップできる Button の作成」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- タップできる Button の作成
- ボタンのスタイル設定
- タップ時にコードを実行する
- ボタンの無効化と有効化