【问题标题】:how to make an uitextfield only accept numbers and commas [duplicate]如何使uitextfield只接受数字和逗号[重复]
【发布时间】:2017-07-18 14:29:19
【问题描述】:

我有一个带有 UITextField 的 alertController。我想要的是,当用户在文本字段中输入数据时,它应该只是数字或逗号。甚至小数点数字也被接受,空格也被接受。

例如23,45, 34,25.4,34

如果用户输入了其他字符,警报视图应该会振动以作为输入错误的响应。

【问题讨论】:

    标签: ios swift string uitextfield uialertcontroller


    【解决方案1】:

    您可以使用文本字段委托来检查输入字符是否属于接受的字符列表。

    使用这个委托:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    //Have a list with valid characters and check if belongs to that list.
    //Return true or false according to validation
    return BOOL
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2011-04-17
      相关资源
      最近更新 更多