目前,我有如下內容:
struct ViewA: View {
@FocusState private var focusedField: Bool
var body: some View {
ViewB(focusedField: $focusedField)
// some other views that read focusedField...
}
}
struct ViewB: View {
@State var focusedField: FocusState<Bool>.Binding
var body: some View {
Button(action: {
focusedField = true // ERROR: Cannot assign value of type 'Bool' to type 'FocusState<Bool>'
})
// ...
}
}
似乎我可以focusedField毫無問題地傳遞下去,但無法更新其值。如何解決這個問題?
uj5u.com熱心網友回復:
而不是直接設定focusedField,使用wrappedValue屬性。
struct ViewA: View {
@FocusState private var focusedField: Bool
var body: some View {
ViewB(focusedField: $focusedField)
// some other views that read focusedField...
}
}
struct ViewB: View {
var focusedField: FocusState<Bool>.Binding
var body: some View {
Button(action: {
focusedField.wrappedValue = true /// assign `wrappedValue`
}) {
Text("Click me!") /// Side note: you need a label for the button too, otherwise your code won't compile
}
// ...
}
}
uj5u.com熱心網友回復:
為了一致性,也許最好使用屬性包裝器@FocusedState.Binding。
struct ViewA: View {
@FocusState private var focusedField: Bool
var body: some View {
ViewB(focusedField: $focusedField)
// some other views that read focusedField...
}
}
struct ViewB: View {
@FocusState.Binding var focusedField: Bool
var body: some View {
Button(action: {
focusedField = true
}) {
Text("Tap me")
}
}
}
這允許做focusedField = true而不是focusedField.wrappedValue = true
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346657.html
標籤:迅速
上一篇:當它們回傳null/none時,如何為刮取的結果設定默認值
下一篇:單擊按鈕時的圖片和文字。用戶界面
