【问题标题】:Character Count Not Working on 2nd Text Field字符数在第二个文本字段上不起作用
【发布时间】:2021-05-25 20:57:02
【问题描述】:

我正在构建一个简单的欢迎页面,其中包含姓名和年龄文本输入字段。我能够成功地限制名称字段上的字符输入计数,但是在年龄字段上使用相同的逻辑根本不起作用。想法?

class WelcomeViewController: UIViewController, UITextFieldDelegate {


@IBOutlet weak var nameTextField: UITextField!

@IBOutlet weak var ageTextField: UITextField!



override func viewDidLoad() {
    super.viewDidLoad()
    
    nameTextField.delegate = self
    ageTextField.delegate = self
    
}


// Character Count Code UITextField
func textField(_ nameTextField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    // get the current text, or use an empty string if that failed
    let currentText = nameTextField.text ?? ""

    // attempt to read the range they are trying to change, or exit if we can't
    guard let stringRange = Range(range, in: currentText) else { return false }

    // add their new text to the existing text
    let updatedText = currentText.replacingCharacters(in: stringRange, with: string)

    // make sure the result is under # characters
    return updatedText.count <= 30
}

func textField2(_ ageTextField: UITextField, shouldChangeCharactersIn range2: NSRange, replacementString string2: String) -> Bool {
    // get the current text, or use an empty string if that failed
    let currentText2 = ageTextField.text ?? ""

    // attempt to read the range they are trying to change, or exit if we can't
    guard let stringRange2 = Range(range2, in: currentText2) else { return false }

    // add their new text to the existing text
    let updatedText2 = currentText2.replacingCharacters(in: stringRange2, with: string2)

    // make sure the result is under # characters
    return updatedText2.count <= 2
}

【问题讨论】:

    标签: ios swift xcode textfield


    【解决方案1】:

    委托方法具有系统调用的特定签名 (textField(_:shouldChangeCharactersIn:replacementString:))。您的第一个函数使用该模式。

    您的第二个目前没有,因为您已将其命名为 textField2 - 系统没有理由知道调用以该前缀开头的内容。

    相反,您可能应该使用单个函数,然后根据将 UITextField 发送到第一个参数的哪个函数,使其行为不同:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField == nameTextField {
          //content
        }
        if textField == ageTextField {
          //content
        }
    }
    

    这显然可能是一个else if,或者如果您限制在两个文本字段中,甚至只是一个else。在 if 块中,您可以调用单独的专用函数,或者将所有逻辑保留在 if 块中。

    switch 为例:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        switch textfield {
        case nameTextField:
          //content
        case ageTextField:
          //content
        default:
          return true
        }
    }
    

    【讨论】:

    • 谢谢,我认为这是在正确的轨道上......但我对函数很陌生 - 当我在“if textField......”语句中移动代码时 - 我收到这个错误在函数的最后。我将如何正确关闭该功能?错误 = "预期返回 'Bool' 的函数中缺少返回"
    • 如果没有if 块被击中(它不应该......),你必须返回一个默认值。因此,例如,您可以在两个 if 块之外的函数末尾添加 return true
    • 最好使用开关
    • @LeoDabus 可能是个好主意。编辑以包含switch 的示例
    猜你喜欢
    • 2019-05-08
    • 2019-02-09
    • 2010-12-20
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 2016-07-30
    • 2012-03-24
    • 1970-01-01
    相关资源
    最近更新 更多