SwiftUI-如何通过单击键盘上的返回按钮在TextField中导航? [英] SwiftUI - How to navigate through TextFields by clicking on return button from keyboard?

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

问题描述

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

1)在Swift中,我们可以像这样从情节提要中将TextField的返回键(文本输入特征)设置为Next?为此,在SwiftUI中使用哪个修饰符?


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

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?


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

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

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

Can anyone help with this Or any other alternative to perform this feature?

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

My question is about SwiftUI not UIKit one :)

任何帮助将不胜感激!

推荐答案

要解决两个问题,您需要使用 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-如何通过单击键盘上的返回按钮在TextField中导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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