@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
}
}
}
}
why this sample code not working on WatchOS 8.3? It's copied from Apple Developer Documentation.
Another example is just as abnormal
struct LoginForm {
enum Field: Hashable {
case username
case password
}
@State private var username = ""
@State private var password = ""
@FocusState private var focusedField: Field?
var body: some View {
Form {
TextField("Username", text: $username)
.focused($focusedField, equals: .username)
SecureField("Password", text: $password)
.focused($focusedField, equals: .password)
Button("Sign In") {
if username.isEmpty {
focusedField = .username
} else if password.isEmpty {
focusedField = .password
} else {
handleLogin(username, password)
}
}
}
}
}
https://developer.apple.com/documentation/swiftui/focusstate?changes=latest_minor https://developer.apple.com/documentation/swiftui/link/focused(_:)?changes=latest_minor