将textField条目限制为设置长度的数字条目

时间:2015-07-22 02:04:27

标签: ios xcode swift

我试图限制3个不同的文本字段,以便您只能在其中输入数字,以便您可以输入的数字位数有限。

这是我的代码:

import Foundation
import UIKit
import Darwin

class View3on3 : UIViewController, UITextFieldDelegate {


@IBOutlet weak var APTeams: UITextField!
@IBOutlet weak var APRounds: UITextField!
@IBOutlet weak var APBreakers: UITextField!


override func viewDidLoad()
{
    super.viewDidLoad()
    initializeTextFields()
}


func initializeTextFields()
{
    APTeams.delegate = self
    APTeams.keyboardType = UIKeyboardType.NumberPad

    APRounds.delegate = self
    APRounds.keyboardType = UIKeyboardType.NumberPad

    APBreakers.delegate = self
    APBreakers.keyboardType = UIKeyboardType.NumberPad
}


override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}


@IBAction func userTappedBackground(sender: AnyObject)
{
    view.endEditing(true)
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    if string.characters.count == 0 {
        return true
    }

    let prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: String())

    switch textField {
    case APTeams:
        return prospectiveText.containsOnlyCharactersIn("0123456789") && count(prospectiveText) <= 3

    case APRounds:
        return prospectiveText.containsOnlyCharactersIn("0123456789") && count(prospectiveText) <= 1

    case APBreakers:
        return prospectiveText.containsOnlyCharactersIn("0123456789") && count(prospectiveText) <= 3

    default:
        return true
    }

}

}

它会在此行引发错误:

let prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: String())

说出来 &#39;字符串&#39?;不可转换为NSString

我按照本网站的教程: http://www.globalnerdy.com/2015/04/27/how-to-program-an-ios-text-field-that-takes-only-numeric-input-or-specific-characters-with-a-maximum-length/

我做错了什么?

PS。调用NumberPad的代码工作正常。当我删除代码以限制位数时,它工作正常。

5 个答案:

答案 0 :(得分:1)

  

试试这个swift 2. textField.text是一个可选值,所以你需要在使用之前解开它...这就是你得到Stirng的原因?无法转换为NSString。

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

    if let textFieldText = textField.text {

        let textAfterUpdate = textFieldText as NSString

        print(textAfterUpdate)

    }


 return true

}

答案 1 :(得分:1)

或者你可以试一试,制作一个方法并在视图中调用它来加载:

func initializeTextFields({
digitsOnlyTextField.delegate = self
digitsOnlyTextField.keyboardType = UIKeyboardType.ASCIICapable

然后,您可以将文本字段硬编码为:

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

case digitsOnlyTextField:
return prospectiveText.containsOnlyCharactersIn("012346789")

给这个镜头拍我一些反馈,祝你好运!

答案 2 :(得分:0)

试试这个,我在你的代码中做了一些改动..

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

    let invalidCharSet : NSCharacterSet = NSCharacterSet(charactersInString: "0123456789").invertedSet

    let filteredString : NSString = ((string.componentsSeparatedByCharactersInSet(invalidCharSet)) as NSArray).componentsJoinedByString("")

    var allow : Bool = false

    if (string == filteredString) {

        allow = true
    }

    let newLength = count(textField.text) + count(string) - range.length

    switch textField {

    case APTeams:

        if (newLength > 3) {

            allow = false
        }

        return allow

    case APRounds:

        if (newLength > 1) {

            allow = false
        }

        return allow

    case APBreakers:

        if (newLength > 3) {

            allow = false
        }

        return allow

    default:
        return true
    }
}

答案 3 :(得分:0)

您无法将选项投射到NSString。由于您使用的是swift 2.0,因此可以保护这样的值:

guard let text = textField.text else { return false }
let prospectiveText = (text as NSString).stringByReplacingCharactersInRange(range, withString: string)

答案 4 :(得分:0)

我是您提到的教程的作者。我为Swift 1编写了它,代码在Swift 2下断开。这是修复:

let currentText = textField.text ?? ""
let prospectiveText = (currentText as NSString).stringByReplacingCharactersInRange(range, withString: string)