【问题标题】:How to have UITextField allow only integers with Swift 3?如何让 UITextField 只允许使用 Swift 3 的整数?
【发布时间】:2017-01-08 01:30:14
【问题描述】:

这是我的代码:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var maximumNumber: UITextField!
    @IBAction func playButtonPressed(_ sender: Any) {
        maximumNumber.isHidden = true
        guessTextField.isHidden = false
    }
    @IBOutlet weak var guessTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        guessTextField.isHidden = true
        maximumNumber.keyboardType = .numberPad
        guessTextField.keyboardType = .numberPad

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

            // Create an `NSCharacterSet` set which includes everything *but* the digits
            let inverseSet = NSCharacterSet(charactersIn:"0123456789").inverted

            // At every character in this "inverseSet" contained in the string,
            // split the string up into components which exclude the characters
            // in this inverse set
            let components = string.components(separatedBy: inverseSet)

            // Rejoin these components
            let filtered = components.joined(separator: "")  // use join("", components) if you are using Swift 1.2

            // If the original string is equal to the filtered string, i.e. if no
            // inverse characters were present to be eliminated, the input is valid
            // and the statement returns true; else it returns false
            return string == filtered  
        }
    }
}

我希望文本字段 maximumNumber 和guessTextField 只允许整数。我尝试使用在这里找到的其他函数和委托,但它们无法工作或者我得到编译器错误。

【问题讨论】:

  • 我尝试过使用我在这里找到的其他函数和委托,但它们无法工作或者我得到编译器错误但是在代码中 i> 显示,您正在做 nothing 来限制用户可以在此文本字段中输入的内容。什么都不会产生。显示一些代码。
  • 由于它们不起作用,我已经删除了它们。
  • 但在这种情况下,您只是要求某人从头开始为您编写整个代码。这不太合适。
  • 我们不会跑去看看一些 pastebin。在此处显示您的代码作为您问题的一部分。

标签: ios swift swift3 uitextfield


【解决方案1】:

这将有所帮助 - Swift 3

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    guard NSCharacterSet(charactersInString: "0123456789").isSupersetOfSet(NSCharacterSet(charactersInString: string)) else {
        return false
    }
    return true
}

【讨论】:

    【解决方案2】:

    看起来您的 textfield(shouldChangeCharactersIn:replacementString:) 方法嵌套在 viewDidLoad 中。

    这样的委托方法必须位于委托类的顶层,否则它们将不会被调用。

    将你的方法移动到视图控制器的顶层,然后添加一个断点并确保它被调用。

    【讨论】:

      【解决方案3】:

      迅速 3

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

      上面是应该在视图控制器中但在任何方法之外的方法,它不应该在viewDidLoad()方法中

      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
          if textField==yourTextFieldOutlet {
                      if(CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: yourTextFieldOutlet.text!))){
      //if numbers only, then your code here
                      }
                      else{
                      showAlert(title: "Error",message: "Enter Number only",type: "failure")
                      }
                  }
          return true
          }
      

      【讨论】:

        【解决方案4】:
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
            if textField == ageTextField {
                guard NSCharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
                    return false
                }
            }
            return true
        }
        

        【讨论】:

          【解决方案5】:
          extension JobCreationViewController: UITextFieldDelegate {
              func textField(_ textField: UITextField,
                             shouldChangeCharactersIn range: NSRange,
                             replacementString string: String) -> Bool {
                  guard NSCharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
                      return false
                  }
                  return true
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2021-10-14
            • 1970-01-01
            • 2012-10-08
            • 2014-04-11
            • 2011-11-24
            • 1970-01-01
            • 2016-06-30
            • 2020-01-09
            • 2018-01-22
            相关资源
            最近更新 更多