本文介绍了SwiftUI:如何让 TextField 成为第一响应者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 SwiftUI 代码:

struct ContentView : 查看 {@State var 显示TextField = false@State var text = ""var主体:一些视图{返回 VStack {如果显示TextField {文本字段($文本)}按钮(动作:{ self.showingTextField.toggle()}){文本(显示")}}}}

我想要的是当文本字段变得可见时,使文本字段成为第一响应者(即接收焦点并弹出键盘).

解决方案

iOS 15.0+

macOS 12.0+,

Mac 催化剂 15.0+,

tvOS 15.0+,

watchOS 8.0+

如果您只有一个 TextField,请使用 focused(_:).

专注(_:)

通过将其焦点状态绑定到给定的 Boolean 状态值来修改此视图.

struct NameForm: View {@FocusState 私有变量 isFocused: Bool@State 私有变量名 = "";var主体:一些视图{文本字段(名称",文本:$name).focused($isFocused)按钮(提交"){如果 name.isEmpty {isFocued = 真}}}}

如果您有多个 TextField,请使用 focused(_:equals:).

专注(_:equals:)

通过将其焦点状态绑定到给定的状态值来修改此视图.

struct LoginForm:查看{枚举字段:Hashable {案例用户名字段案例密码字段}@State private var username = "";@State private var password = "";@FocusState 私有变量focusedField:字段?var主体:一些视图{形式 {文本字段(用户名",文本:$用户名).focused($focusedField, 等于: .usernameField)SecureField(密码",文本:$password).focused($focusedField, 等于: .passwordField)按钮(登录"){如果用户名.isEmpty {focusField = .usernameField} else if password.isEmpty {focusField = .passwordField} 别的 {句柄登录(用户名,密码)}}}}}

SwiftUI 文档

Here's my SwiftUI code:

struct ContentView : View {

    @State var showingTextField = false
    @State var text = ""

    var body: some View {
        return VStack {
            if showingTextField {
                TextField($text)
            }
            Button(action: { self.showingTextField.toggle() }) {
                Text ("Show")
            }
        }
    }
}

What I want is when the text field becomes visible, to make the text field become the first responder (i.e. receive focus & have the keyboard pop up).

解决方案

iOS 15.0+

macOS 12.0+,

Mac Catalyst 15.0+,

tvOS 15.0+,

watchOS 8.0+

Use focused(_:) if you have a single TextField.

struct NameForm: View {

    @FocusState private var isFocused: Bool

    @State private var name = ""

    var body: some View {
        TextField("Name", text: $name)
            .focused($isFocused)

        Button("Submit") {
            if name.isEmpty {
                isFocued = true
            }
        }
    }
}

Use focused(_:equals:) should you have multiple TextFields.

struct LoginForm: View {
    enum Field: Hashable {
        case usernameField
        case passwordField
    }

    @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: .usernameField)

            SecureField("Password", text: $password)
                .focused($focusedField, equals: .passwordField)

            Button("Sign In") {
                if username.isEmpty {
                    focusedField = .usernameField
                } else if password.isEmpty {
                    focusedField = .passwordField
                } else {
                    handleLogin(username, password)
                }
            }
        }
    }
}

SwiftUI Documentation

这篇关于SwiftUI:如何让 TextField 成为第一响应者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 09:22