【问题标题】:Swift prevent "0" being first character for 1 specific uiTextFieldSwift 防止“0”成为 1 个特定 uiTextField 的第一个字符
【发布时间】:2020-11-19 04:19:55
【问题描述】:

所以我在网上搜索“func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool” 来格式化 textField。我发现这种方法可以防止“0”作为我的文本字段中的第一个字符。它有效,但实际上不是我想要的方式。

问题是,在我的注册视图中,有 4 个 uiTextfield,其中包含:

  • 姓名
  • 电话号码
  • 别针
  • 确认密码

我只想防止“0”作为我的PhoneNum TextField 的第一个字符。但是使用这段代码,我有点将所有这 4 个文本字段放入防止“0”作为第一个字符。

这是我的代码:

@IBOutlet weak var textFullName: SkyFloatingLabelTextField!
@IBOutlet weak var textPhoneNumber: SkyFloatingLabelTextField!
@IBOutlet weak var textPIN: SkyFloatingLabelTextField!
@IBOutlet weak var textConfirmPIN: SkyFloatingLabelTextField!


func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    // I try this one
    if string == "0" {
        if textField.text!.count == 0 {
            return false
        }
    }
    
    // i also try to change the "textField" into "textPhoneNumber"
    if string == "0" {
        if textPhoneNumber.text!.count == 0 {
            return false
        }
    }
    

   // both of those method wont work
  
    return true
}

谁能告诉我如何防止我的 textPhoneNumber 不能输入“0”作为它的第一个字符?使用这种方法,我只将所有文本字段放入不能输入“0”作为其第一个字符。

谢谢大家

【问题讨论】:

  • if textField == textPhoneNumber { do the stuff check } else { return true } 也可以写成guard elseguard textField == textPhoneNumber else { return true } do the stuff check
  • 另外,在进行测试之前,您应该发现生成的字符串是什么。请参阅stackoverflow.com/questions/25621496/… 的回复。该方法处理用户键入“90”,然后返回并删除“9”的情况。

标签: ios swift uitextfield


【解决方案1】:

UITextFieldDelegate 方法将为视图控制器设置为委托的所有文本字段调用。

大概您的textField(_:shouldChangeCharactersIn:replacementString:) 方法正在为您的所有4 个SkyFloatingLabelTextField 对象调用(我假设它是UITextField 的自定义子类。)

如果您希望不同文本字段的行为不同,则需要编写代码来分别处理每种情况。像这样的:

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

    switch textField {
        case textPhoneNumber:
            //Special case code to handle the phone number field
            if string == "0" {
                if textField.text!.count == 0 {
                   return false
                }
                return true
            }

        case textFullName:
            //Special case code to handle the name field (if needed.)
        default:
            //Shared code to handle the other fields the same way
    }
}

【讨论】:

    【解决方案2】:

    您可以使用等号== 符号直接检查textField 是否为textPhoneNumber

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField == textPhoneNumber {
            // do textPhoneNumber stuff
        } else {
            // others
        }
        return true
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2019-09-07
      • 2014-07-08
      • 2018-01-23
      • 1970-01-01
      相关资源
      最近更新 更多