从一个文本字段转到另一个

Posted

技术标签:

【中文标题】从一个文本字段转到另一个【英文标题】:Going from one textfield to another 【发布时间】:2019-07-20 06:01:12 【问题描述】:

我有四个分开的UITexFields,每个一个字符,用于验证目的

如下图所示:

( - - - - )

我需要一种方法,当用户在一个文本字段中键入时,它会转到下一个文本字段,而无需单击另一个文本字段。

或者,如果有其他方法可以实现此功能,请告诉我

【问题讨论】:

【参考方案1】:

不要将所有文本字段都显示在视图中,而只需使用 1 个文本字段(仅在代码中 - 不在视图层次结构中),它将从用户那里获取整个代码。并在屏幕上取 4 个按钮 - 谁的标题将是基于索引的文本字段字符。所以通过这种方式你可以实现它。

【讨论】:

【参考方案2】:

好问题老板 对于你的问题,我有一个解决方案。 试试这个... 注意*根据您的要求,您可以从 textFieldDidChange 方法操作文本字段...

class ViewController: UIViewController ,UITextFieldDelegate 
@IBOutlet var text1 : UITextField!
@IBOutlet var text2 : UITextField!
@IBOutlet var text3 : UITextField!
@IBOutlet var text4 : UITextField!

override func viewDidLoad() 
    super.viewDidLoad()
   //TAG FOR IDENTITY
    text1.tag = 1
    text2.tag = 2
    text3.tag = 3
    text4.tag = 4

    text1.becomeFirstResponder()

    text1.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
    text2.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
    text3.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
    text4.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)




@objc func textFieldDidChange(_ sender : UITextField)

    if sender.tag == 1
        text1.resignFirstResponder()
        text2.becomeFirstResponder()

    else if sender.tag == 2
        text2.resignFirstResponder()
        text3.becomeFirstResponder()
    else if sender.tag == 3
        text3.resignFirstResponder()
        text4.becomeFirstResponder()
    else if sender.tag == 4
        text4.resignFirstResponder()
    else
        print("Done")
    

【讨论】:

【参考方案3】:

您可以使用 4 个文本字段并在editingChanged 上移至下一个文本字段,如下所示:

@IBOutlet weak var code1: UITextField!
@IBOutlet weak var code2: UITextField!
@IBOutlet weak var code3: UITextField!
@IBOutlet weak var code4: UITextField!


func addTargets() 
    code1.addTarget(self, action: #selector(editingChanged), for: UIControl.Event.editingChanged)
    code2.addTarget(self, action: #selector(editingChanged), for: UIControl.Event.editingChanged)
    code3.addTarget(self, action: #selector(editingChanged), for: UIControl.Event.editingChanged)
    code4.addTarget(self, action: #selector(editingChanged), for: UIControl.Event.editingChanged)



@objc func editingChanged(sender: UITextField) 
    switch (sender) 
        case code1:
            code2.becomeFirstResponder()
        case code2:
            code3.becomeFirstResponder()
        case code3:
            code4.becomeFirstResponder()
        default:

            // your code input would be done here, you could handle for example code verification

            break;
    

【讨论】:

【参考方案4】:

首先让您的文本字段在 UIViewController 类中全局可访问。

确保您的 UIViewController 符合委托 UITextFieldDelegate。

然后在你的 UIViewController 中为每个 textField 设置委托:

textField.delegate = self

对于以下代码,我以以下 4 个文本字段为例。用户从左到右输入电话号码。 textField 名称依次为 ext、areaCode、middleThree、finalFour。

当用户按下返回键时移动到下一个字段

// Return text field upon pressing return
func textFieldShouldReturn(_ textField: UITextField) -> Bool 
    if textField == self.ext 
        self.areaCode?.becomeFirstResponder()
     else 
        self.areaCode?.resignFirstResponder()
    
    if textField == self.areaCode 
        self.middleThree?.becomeFirstResponder()
     else 
        self.middleThree?.resignFirstResponder()
    
    if textField == self.middleThree 
        self.finalFour?.becomeFirstResponder()
     else 
        self.finalFour?.resignFirstResponder()
    
    if textField == self.finalFour 
        self.finalFour?.resignFirstResponder()
    
    return true

完成字符计数后移至下一个字段(例如,一旦用户在文本字段中输入 4 个字符,则转到下一个。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString: String) -> Bool 

    var characterCountLimit = 0;

    switch textField 
    case self.ext:
        characterCountLimit = 3
    case self.areaCode:
        characterCountLimit = 3
    case self.middleThree:
        characterCountLimit = 3
    case self.finalFour:
        characterCountLimit = 4
    default:
        characterCountLimit = 1000
    

    // Determine how many characters in the string after the change happens.
    let startingLength = textField.text?.count ?? 0
    let lengthToAdd = replacementString.count
    let lengthToReplace = range.length

    // This might be called when the user presses backspace, but the count is has reached the limit. Thus, we have to determine the new length.

    let newLength = startingLength + lengthToAdd - lengthToReplace
    // If the user did not press backspace, but continued typing, move to the next field. Take the most recently typed character and enter it as text in the new field and remove it from the old field.
    if newLength > characterCountLimit 
        switch textField 
        case self.ext:
            self.areaCode.text = String(describing: (replacementString.last)!)
            self.areaCode?.becomeFirstResponder()
        case self.areaCode:
            self.middleThree.text = String(describing: (replacementString.last)!)
            self.middleThree?.becomeFirstResponder()
        case self.middleThree:
            self.finalFour.text = String(describing: (replacementString.last)!)
            self.finalFour?.becomeFirstResponder()
        case self.finalFour:
            break
            // Do nothing. This is handled by the editing change delegate method.
        default:
            break
        
     else if replacementString.isEmpty && range.lowerBound == 0 && range.upperBound == 1 
        // If the user is pressing backspace and you want to return to the previous field
        switch textField 
        case self.finalFour:
            self.finalFour.text = ""
            self.middleThree?.becomeFirstResponder()
        case self.middleThree:
            self.middleThree.text = ""
            self.areaCode?.becomeFirstResponder()
        case self.areaCode:
            self.areaCode.text = ""
            self.ext?.becomeFirstResponder()
        default:
            break
        
    
    return newLength <= characterCountLimit

【讨论】:

以上是关于从一个文本字段转到另一个的主要内容,如果未能解决你的问题,请参考以下文章

PYTHON + SELENIUM(CHROME):如何从当前网址中提取特定文本并使用提取的文本转到另一个文本?

将文本值从一个视图控制器传送到另一个视图控制器

无需单击按钮即可将数据从一个文本字段传输到另一个文本字段

如何在swift ios中自动将光标从一个文本字段移动到另一个文本字段?

将文本输入从一个字段复制到另一个字段

当键盘从自己的文本字段切换到另一个文本字段时,搜索栏右键“取消”被禁用