修复拼写页闪退:改用 becomeFirstResponder 管理焦点

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-07-18 21:32:21 +08:00
parent 120dac0ec9
commit 7d123afe79
@@ -5,6 +5,7 @@ import UIKit
struct KeystrokeCaptureField: UIViewRepresentable {
let onChar: (Character) -> Void
let onBackspace: () -> Void
@Binding var isActive: Bool
func makeUIView(context: Context) -> UITextField {
let tf = UITextField()
@@ -19,7 +20,13 @@ struct KeystrokeCaptureField: UIViewRepresentable {
Coordinator(onChar: onChar, onBackspace: onBackspace)
}
func updateUIView(_ uiView: UITextField, context: Context) {}
func updateUIView(_ uiView: UITextField, context: Context) {
if isActive {
uiView.becomeFirstResponder()
} else {
uiView.resignFirstResponder()
}
}
class Coordinator: NSObject, UITextFieldDelegate {
let onChar: (Character) -> Void
@@ -61,7 +68,7 @@ struct SpellingWordInput: View {
let blankPositions: [Int]
@Binding var currentBlankIndex: Int
@Binding var filledBlanks: [String]
@FocusState private var isFocused
@State private var isFocused = false
private let chars: [String]
@@ -85,11 +92,10 @@ struct SpellingWordInput: View {
guard currentBlankIndex > 0 else { return }
currentBlankIndex -= 1
filledBlanks[currentBlankIndex] = ""
}
},
isActive: $isFocused
)
.frame(width: 0, height: 0)
.opacity(0.01)
.focused($isFocused)
HStack(spacing: 4) {
ForEach(Array(chars.enumerated()), id: \.offset) { index, ch in