我如何知道 SwiftUI 按钮是否已启用/禁用? [英] How can I know if a SwiftUI Button is enabled/disabled?
问题描述
SwiftUI 按钮没有 isEnabled
属性.我怎么知道它是否已启用?
There is no isEnabled
property for a SwiftUI button. How can i tell if it is enabled?
在普通的 UIKit 中,我会简单地做
In regular UIKit, i would simply do
if button.isEnabeld == true {
} else {
}
但没有等效的 SwiftUI.
but there is no SwiftUI equivalent.
推荐答案
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.
这篇关于我如何知道 SwiftUI 按钮是否已启用/禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!