0
@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

qun xue
  • 1
  • 1
  • What is the expected behaviour? could you please create a short video or gif of the problem? It will help understand the problem. – Jarvis The Avenger Dec 20 '21 at 11:09
  • It looks like a bug to me! – Jarvis The Avenger Dec 20 '21 at 11:30
  • I think the performance should be: when I press the button, textfield will get the focus and open the input interface. But now, after pressing the button, there is no response. – qun xue Dec 20 '21 at 11:41
  • Are you sure `isUserNameAvailable` is returning the correct `Bool`? You only set your focus if it returns `false`. – Yrb Dec 20 '21 at 15:45
  • Sure, I removed the IF statement and set the usernameFieldIsFocused for true. But the behavior is still the same. Are you all normal? The same is not working for another example. What did I do wrong, Please? – qun xue Dec 21 '21 at 13:57

0 Answers0