如何让UITextField只允许整数与Swift 3?

时间:2017-01-08 01:30:14

标签: ios swift swift3 uitextfield

这是我的代码:

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只允许整数。我已经尝试过使用我在这里找到的其他函数和委托,但它们无法正常工作或者出现编译错误。

5 个答案:

答案 0 :(得分:2)

这会有所帮助--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
}

答案 1 :(得分:0)

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

这样的委托方法必须位于代表课程的顶层,否则他们不会被调用。

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

答案 2 :(得分:0)

swift 3

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

上面的方法应该在viewcontroller中,但在任何方法之外,它不应该在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
    }

答案 3 :(得分:0)

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
}

答案 4 :(得分:0)

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
    }
}