Discussion
議論
Use this modifier to cause the view to receive focus whenever the the condition
value is true
. You can use this modifier to observe the focus state of a single view, or programmatically set and remove focus from the view.
In the following example, a single TextField
accepts a user’s desired username
. The text field binds its focus state to the Boolean value usernameFieldIsFocused
. A “Submit” button’s action verifies whether the name is available. If the name is unavailable, the button sets usernameFieldIsFocused
to true
, which causes focus to return to the text field, so the user can enter a different name.
@State private var username: String = ""
@FocusState private var usernameFieldIsFocused: Bool
@State private var showUsernameTaken = false
var body: some View {
VStack {
TextField("Choose a username.", text: $username)
.focused($usernameFieldIsFocused)
if showUsernameTaken {
Text("That username is taken. Please choose another.")
}
Button("Submit") {
showUsernameTaken = false
if !isUserNameAvailable(username: username) {
usernameFieldIsFocused = true
showUsernameTaken = true
}
}
}
}
To control focus by matching a value, use the focused(_:equals:)
method instead.