且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何知道 SwiftUI 按钮是否已启用/禁用?

更新时间:2023-12-05 09:39:58

SwiftUI 的整个理念,就是避免重复事实来源.您需要以不同的方式思考,并考虑真相的来源.这是您需要查找按钮状态的地方.不是来自按钮本身.

The whole idea of SwiftUI, is to avoid duplication of the source of truth. You need to think differently, and consider where the source of truth is. This is where you need to go to find out the button's state. Not from the button itself.

在Data Flow Through SwiftUI"中,在 30:50 分钟,他们解释说每条数据都有一个单一事实来源.如果您的按钮从某些@Binding、@State、@EnvironmentObject 等获取其状态,则您的 if 语句也应从同一位置获取该信息,而不是从按钮中获取.

In "Data Flow Through SwiftUI", at minute 30:50, they explain that every piece of data has a single source of truth. If your button gets its state from some @Binding, @State, @EnvironmentObject, etc, your if statement should get that information from the same place too, not from the button.