【发布时间】:2021-05-11 15:34:31
【问题描述】:
我有一个带有嵌套迷你表单的表单(用于添加项目),因此您可以添加更多项目(从而向表单动态添加更多字段)。我想更改用户当前正在输入的字段的样式(焦点状态)。
我见过一些solutions using state vars to keep track of each field,但由于我的字段是动态添加的,因此跟踪未知数量的变量听起来比应该的要麻烦。
TextField("", text: $viewModel.item.name, onEditingChanged: {
if $0 {
//Put something here to change the border or background color directly, maybe reference to the field id or tag?
}
})
.modifier(TextInputStyle(isDisabled : false, isEditing: something)) // Or somehow pass a value here without having to make a state variable
更新:感谢一些帮助,我离得更近了,但现在我在通过按钮(切换isDisabled)以编程方式禁用该字段时遇到了一些错误。
在控制台中,我得到了这个:
=== AttributeGraph:通过属性 1864952 检测到循环 === === AttributeGraph:通过属性 1864952 检测到循环 === === AttributeGraph:通过属性 1864952 检测到循环 === === AttributeGraph:通过属性 1866776 检测到循环 === === AttributeGraph:通过属性 1865304 检测到循环 === === AttributeGraph:通过属性 1865304 检测到循环 === === AttributeGraph:通过属性 1862584 检测到循环 === === AttributeGraph:通过属性 1847528 检测到循环 === === AttributeGraph:通过属性 1847528 检测到循环 === === AttributeGraph:通过属性 1865304 检测到循环 === === AttributeGraph:通过属性 1878320 检测到循环 === === AttributeGraph:通过属性 1878200 检测到循环 === === AttributeGraph:通过属性 1878320 检测到循环 === === AttributeGraph:通过属性 1878200 检测到循环 === 2021-02-08 12:52:20.918323-0600 App [23659:862912] [SwiftUI] 在视图更新期间修改状态,这将导致未定义的行为。
【问题讨论】:
-
你能把它和文字比较一下吗?如果 $viewModel.item.name.count > 1...
标签: swiftui