SwiftUI-如何通过单击键盘上的返回按钮在TextField中导航?

Anj*_*iya 6 textfield ios swiftui

我正在使用SwiftUI的TextField View。我主要有2个问题,

1)在Swift中,我们可以像这样从情节提要中将TextField的Return Key(Text Input Traits)设置Next为TextField吗?为此,在SwiftUI中使用哪个修饰符?

在此处输入图片说明

2)我有两个文本字段,当我单击键盘上的返回/下一步按钮时,如何导航到下一个文本字段?

任何人都可以帮助执行此功能或其他任何替代方法吗?

我的问题是关于SwiftUI而不是UIKit :)

任何帮助将非常感激!!

Raz*_*ick 6

要解决你的两个问题,你需要工作,UIKit的SwiftUI。首先,您需要使用UIViewRepresentable定制TextField。这是用于测试目的的示例代码,尽管该代码不是那么优雅。我敢打赌,将会有一个更强大的解决方案。

  1. 在自定义TextFieldType内,已设置Keyboard返回类型。
  2. 通过使用对象绑定和委托方法 textFieldShouldReturn,View可以通过更新绑定变量来使键盘聚焦。

这是示例代码:

import SwiftUI

struct KeyboardTypeView: View {
    @State var firstName = ""
    @State var lastName = ""
    @State var focused: [Bool] = [true, false]

    var body: some View {
        Form {
            Section(header: Text("Your Info")) {
                TextFieldTyped(keyboardType: .default, returnVal: .next, tag: 0, text: self.$firstName, isfocusAble: self.$focused)
                TextFieldTyped(keyboardType: .default, returnVal: .done, tag: 1, text: self.$lastName, isfocusAble: self.$focused)
                Text("Full Name :" + self.firstName + " " + self.lastName)
            }
        }
}
}



struct TextFieldTyped: UIViewRepresentable {
    let keyboardType: UIKeyboardType
    let returnVal: UIReturnKeyType
    let tag: Int
    @Binding var text: String
    @Binding var isfocusAble: [Bool]

    func makeUIView(context: Context) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.keyboardType = self.keyboardType
        textField.returnKeyType = self.returnVal
        textField.tag = self.tag
        textField.delegate = context.coordinator
        textField.autocorrectionType = .no

        return textField
    }

    func updateUIView(_ uiView: UITextField, context: Context) {
        if isfocusAble[tag] {
            uiView.becomeFirstResponder()
        } else {
            uiView.resignFirstResponder()
        }
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, UITextFieldDelegate {
        var parent: TextFieldTyped

        init(_ textField: TextFieldTyped) {
            self.parent = textField
        }

        func updatefocus(textfield: UITextField) {
            textfield.becomeFirstResponder()
        }

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

            if parent.tag == 0 {
                parent.isfocusAble = [false, true]
                parent.text = textField.text ?? ""
            } else if parent.tag == 1 {
                parent.isfocusAble = [false, false]
                parent.text = textField.text ?? ""
         }
        return true
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

输出: 在此处输入图片说明

  • 好的解决方案。谢谢。这是一个常见的用例,尤其是在表单中。没有更快速的解决方案来解决这个问题吗? (2认同)

mah*_*han 6

iOS 15.0+

\n

macOS 12.0+、\nMac Catalyst 15.0+、\ntvOS 15.0+、\nwatchOS 8.0+

\n
\n

使用submitLabel(_:)视图修饰符设置视图的提交标签。它需要指定的预定义情况SubmitLabel

\n
\n
\n

使用.next。它定义了一个提交标签,其文本为\xe2\x80\x9cNext\xe2\x80\x9d

\n
\n
\n

用于onFocus(_:)查找修改后的视图层次结构(在本例中为 )TextField何时失去焦点。当出现时,将焦点放在下一个视图上 ( SecureField)

\n
\n
struct LoginForm: View {\n    enum Field: Hashable {\n        case usernameField\n        case passwordField\n    }\n    \n    @State private var username = ""\n    @State private var password = ""\n    @FocusState private var focusedField: Field?\n    \n    var body: some View {\n        Form {\n            TextField("Username", text: $username)\n                .focused($focusedField, equals: .usernameField)\n                .submitLabel(.next)\n                .onFocus { isFocused in\n                    if (!isFocused) {\n                        focusedField = .passwordField\n                    }\n                }\n            \n            SecureField("Password", text: $password)\n                .focused($focusedField, equals: .passwordField)\n                .submitLabel(.done)\n        }\n    }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n\n