如果"Backspace"(退格)出现,我该如何移至上一个UITextField.按下按钮,文本字段为空白,在Swift 4中? [英] How can I move to the previous UITextField if the "Backspace" button is pressed and the text field is blank and in Swift 4?

查看:118
本文介绍了如果"Backspace"(退格)出现,我该如何移至上一个UITextField.按下按钮,文本字段为空白,在Swift 4中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决这个问题.当我使用下面的代码时,我可以从一个文本字段移至下一个文本字段,然后单击退格"按钮,但仅当文本字段中包含文本时才可以.

I am trying to figure this out. When I use the code below, I am able to move from one text field to the next and click the "Backspace" button but ONLY when the text field has text in there.

我的问题:当文本字段为空白时,如何单击退格"按钮并移至上一个文本字段?

My Question: How can I click the "Backspace" button when the text field is blank and move to the previous text field?

第二个问题:如何摆脱UITextField上闪烁的蓝线?

Second Question: How do I get rid of the blinking blue line on the UITextField?

下面是我的代码.任何帮助将不胜感激!

Below is the code that I have. Any help will be greatly appreciated!

@objc func textFieldDidChange(textfield: UITextField) {
    let text = textfield.text!
    if text.utf16.count == 0 {
        switch textfield {
        case textField2:
            textField1.becomeFirstResponder()
            textField1.backgroundColor = UIColor.black
        case textField3:
            textField2.becomeFirstResponder()
            textField2.backgroundColor = UIColor.black
        case textField4:
            textField3.becomeFirstResponder()
            textField3.backgroundColor = UIColor.black
        case textField5:
            textField4.becomeFirstResponder()
            textField4.backgroundColor = UIColor.black
        case textField6:
            textField5.becomeFirstResponder()
            textField5.backgroundColor = UIColor.black
            textField6.resignFirstResponder()
            textField6.backgroundColor = UIColor.black
        default:
            break
        }
    }
    else if text.utf16.count == 1 {
        switch textfield {
        case textField1:
            textField1.backgroundColor = UIColor.black
            textField1.textColor = .white
            textField2.becomeFirstResponder()
            textField2.backgroundColor = UIColor.black
            textField2.textColor = .white
        case textField2:
            textField3.becomeFirstResponder()
            textField3.backgroundColor = UIColor.black
            textField3.textColor = .white
        case textField3:
            textField4.becomeFirstResponder()
            textField4.backgroundColor = UIColor.black
            textField4.textColor = .white
        case textField4:
            textField5.becomeFirstResponder()
            textField5.backgroundColor = UIColor.black
            textField5.textColor = .white
        case textField5:
            textField6.becomeFirstResponder()
            textField6.backgroundColor = UIColor.black
            textField6.textColor = .white
        case textField6:
            textField6.resignFirstResponder()
        default:
            break

        }
    }
}

推荐答案

您应该将UITextField子类化以实现deleteBackward()函数.另外,您应该实现一个协议,该协议具有在调用deleteBackward()并且文本为空时执行的功能.

You should subclass UITextField to implement the deleteBackward() function. Also, you should implement a protocol which has a function that executes when deleteBackward() is called and the text is empty.

免责声明::此代码的来源位于

DISCLAIMER: This code's origin is in the VENTokenField project. It was converted to Swift and tweaked by me.

class BackspaceTextField: UITextField {
    weak var backspaceTextFieldDelegate: BackspaceTextFieldDelegate?

    override func deleteBackward() {
        if text?.isEmpty ?? false {
            backspaceTextFieldDelegate?.textFieldDidEnterBackspace(self)
        }

        super.deleteBackward()
    }
}

protocol BackspaceTextFieldDelegate: class {
    func textFieldDidEnterBackspace(_ textField: BackspaceTextField)
}

与其分别处理每个文本字段,不如在视图控制器中创建文本字段数组并在那里处理第一响应者.如果存在先前的视图控制器,则将其设置为第一响应者(您无需辞职,因为更改后先前的响应者将自动辞职).如果它是数组中的第一个文本字段,则结束编辑.

Instead of handling each text field separately, create an array of text fields in your view controller and handle first responders there. If there is a previous view controller, this sets it as the first responder (you don't need to resign anything, as the previous responder will automatically resign after the change). If it's the first text field in the array, it ends editing.

class ViewController: UIViewController, BackspaceTextFieldDelegate {
    var textField1: BackspaceTextField!
    var textField2: BackspaceTextField!
    [...]

    var textFields: [BackspaceTextField] {
        return [textField1, textField2, [...]]
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        textFields.forEach { $0.backspaceTextFieldDelegate = self }
    }

    func textFieldDidEnterBackspace(_ textField: BackspaceTextField) {
        guard let index = textFields.index(of: textField) else {
            return
        }

        if index > 0 {
            textFields[index - 1].becomeFirstResponder()
        } else {
            view.endEditing(true)
        }
    }
}

这篇关于如果"Backspace"(退格)出现,我该如何移至上一个UITextField.按下按钮,文本字段为空白,在Swift 4中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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