如何通过单击键盘上的返回按钮来浏览 SwiftUI TextFields? [英] How to navigate through SwiftUI TextFields by clicking on return button from keyboard?

查看:30
本文介绍了如何通过单击键盘上的返回按钮来浏览 SwiftUI TextFields?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SwiftUI 的 TextField View.我主要有两个问题:

I'm working with SwiftUI's TextField View. Mainly I have 2 questions:

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


  1. In Swift, we can set Return Key(Text Input Traits) to Next for TextField from storyboard like this right? For this which modifier to use in SwiftUI?


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

I have two Textfields, how to navigate to the next TextField when I click return/next button from keyboard?

任何人都可以使用 SwiftUI(不是 UIKit)帮助解决这个问题吗?或者任何其他替代方法来执行此功能?

Can anyone help with this using SwiftUI (not UIKit)? Or any other alternative to perform this feature?

推荐答案

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

To resolve your two problems, you need to work with UIKit from SwiftUI. First, you need to customized TextField using UIViewRepresentable. Here is the sample code for test purposes though the code is not so elegance. I bet, there will be having a more robust solution.

  1. 在自定义的 TextFieldType 中,Keyboard 返回类型有被设置.
  2. 通过使用对象绑定和委托方法textFieldShouldReturn,View 可以通过更新绑定变量来聚焦键盘.
  1. Inside the customized TextFieldType, the Keyboard return type has been set.
  2. By using object binding and delegate methods textFieldShouldReturn, View can focus the keyboard by updating the binding variables.

这是示例代码:

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
        }

    }
}

输出:

这篇关于如何通过单击键盘上的返回按钮来浏览 SwiftUI TextFields?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆