I'm working with new feature in iOS 16: UIHostingConfiguration. In a content view I put a TextField. I try to manage the TextField's focus. To do that, I've created a @FocusState var. Unfortunately, I can't update .wrappedValue - the wrapped value always is false. Here's my code:
Cell:
cell.configurationUpdateHandler = { [self] (cell, state) in
cell.contentConfiguration = UIHostingConfiguration {
TaskView(task: task, shouldFocus: focusedTask == task)
}
}
View:
struct TaskView: View {
@ObservedObject var task: Task
@FocusState var shouldFocus: Bool
init(task: Task, shouldFocus: Bool) {
self.task = task
self.$shouldFocus.wrappedValue = shouldFocus
print($shouldFocus)
}
var body: some View {
HStack() {
Button("END") {
$shouldFocus.wrappedValue = false
}
TextField("Task", text: $task.title)
.focused($shouldFocus)
}
}
}