【发布时间】:2019-11-19 20:20:08
【问题描述】:
我有一个包含两个文本字段和一个保存按钮的视图。如何根据文本字段的内容更改按钮的状态(我只想在所有文本字段都不为空的情况下启用按钮)?这是我当前的代码:
// The variables associated with text fields
@State var name: String = ""
@State var type: String = ""
// I'd like to associate this variable with
// my button's disabled / enabled state,
// but the function responsible for it doesn't accept bindings
@State var canSave: Bool = false
var body: some View {
Form {
TextField("Name", text: $name)
TextField("Type", text: $type)
Button(action: {
// ...
}, label: {
Text("Save")
})
.disabled(!canSave) // no bindings allowed here; what to use indead?
}
}
我有一个想法,我应该使用最新的 Combine 框架中的 combineLatest。但是无论我尝试用谷歌搜索什么,都会将我引向与 RxSwift 相关的主题,而不是实际的 Combine 实现。
【问题讨论】: