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