0Pricing
SwiftUI Academy · 课时

点击时运行代码

在点击时输出信息、计数并触发逻辑。

点击时运行代码 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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. ⚡

常见问题解答

「点击时运行代码」课时是免费的吗?

是的 — 「点击时运行代码」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。

「点击时运行代码」这节课中我会学到什么?

在点击时输出信息、计数并触发逻辑。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 SwiftUI Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「点击时运行代码」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 SwiftUI Academy 课中编写并运行代码吗?

能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 创建可点击的按钮
  2. 设置按钮样式
  3. 点击时运行代码
  4. 禁用和启用按钮
← 返回 SwiftUI Academy