简体   繁体   English

实时输入字符时,如何获取 UITextfield 的值?

[英]How do I get the value of a UITextfield as characters are being typed in real time?

I am attempting to regulate the input of a UITextfield in real time, meaning as a user is typing.我正在尝试实时调节 UITextfield 的输入,这意味着用户正在输入。 I have this character set that i need to compare to the input string, and while editing if an unwarranted character is typed in, I want to relay an alert.我有这个字符集需要与输入字符串进行比较,并且在编辑时是否输入了无根据的字符,我想中继警报。 Here is my character set :这是我的字符集:

let acceptedChars = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_")

now how do i capture a specific textfield in real time and track its input?现在如何实时捕获特定的文本字段并跟踪其输入?

Try this:尝试这个:

 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        let invalidCharacters = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_").invertedSet
        if let range = string.rangeOfCharacterFromSet(invalidCharacters, options: nil, range:Range<String.Index>(start: string.startIndex, end: string.endIndex)) {
            return false
        }

        return true
    }

You can register your textField for value change event like this您可以像这样为值更改事件注册 textField

textfield.addTarget(self, action:"textFieldDidChange", forControlEvents:UIControlEvents.EditingChanged)

func textFieldDidChange(){
    // put your code
}

It will work for each chracter you have been typed in real time它适用于您实时输入的每个字符

var strings: NSString?

class ViewController: UIViewController,UITextFieldDelegate //set your textfield delegate class ViewController: UIViewController,UITextFieldDelegate //设置你的文本框委托

 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{  if(textField .isEqual(your textfield))
    {
    strings=string;
    let acceptedChars = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_").invertedSet;

     if (strings!.rangeOfCharacterFromSet(acceptedChars.invertedSet).location != NSNotFound)
    {
        return true;
    }
    else
    {
        return false;
    }        
}
else
{
return true;
}
}

use below method使用下面的方法

   func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
    {
        if textField.isEqual(<textField whose value to be copied>)
        {
            <TextField to be updated>.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
        }
    
        return true
    }

An Easy Delegate method and really more efficient is:一个简单的委托方法,真正更有效的是:

func textFieldDidChangeSelection(_ textField: UITextField) {
    print(textField.text)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM