gpt4 book ai didi

SwiftUI 如何更新 FocusState.Binding?

转载 作者:行者123 更新时间:2023-12-05 05:46:22 27 4
gpt4 key购买 nike

我有一个像这样的文本字段包装器:

public struct SUIDoubleField: View {

@Binding
private var value: Double

private let placeholder: String
private let formatter: NumberFormatter

private var focused: FocusState<Bool>.Binding

public init(value: Double, placeholder: String, formatter: NumberFormatter, focused: FocusState<Bool>.Binding, onChange: @escaping (Double) -> Void) {
_value = Binding(
get: { value },
set: { onChange($0) })
self.placeholder = placeholder
self.formatter = formatter
self.focused = focused
}

public var body: some View {
TextField(placeholder, value: $value, formatter: formatter)
.focused(focused)
.textFieldStyle(.roundedBorder)
.keyboardType(.decimalPad)
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Button(LOC(.dismissKeyboardButton)) {
focused = false // Error
}
}
}
}
}

在这里我无法更新 focus 值。我遇到了这两个错误:

Cannot assign to property: 'self' is immutable
Cannot assign value of type 'Bool' to type 'FocusState<Bool>.Binding'

我尝试传递 onDone 回调,并从该组件外部更新 focused 状态。这很好用,但我认为传递绑定(bind)更方便。我该怎么做?

最佳答案

一个值应该分配给包装值而不是绑定(bind)自己,比如

  Button(LOC(.dismissKeyboardButton)) {
focused.wrappedValue = false // << here !!
}

关于SwiftUI 如何更新 FocusState.Binding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71202500/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com