I newbie on Xcode and programming for MacOS application. Let me help please. I can't understand how write action (or event i don't know) for create limit character set and checking NSTextField on compatible char (0-9, e, .). I read how create char set (NSCharacterSet) but can't what need next. I want forbidding input wrong chars on my NSTextField. How work processing keyboard events for NSTextField and other events in Objective-C? Like example.
1
votes
2 Answers
3
votes
1
votes
I found a post on Cocoadev that will help you. (http://lists.apple.com/archives/cocoa-dev/2006/Aug/msg01492.html )
You'll want to register for a notification to be notified when the text changes, as such:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(textDidChange:)
name: NSControlTextDidChangeNotification
object: textField];
Then in your textDidChange: method (or whatever you name it), you can modify the input. For instance, to ensure uppercase:
[[textField setStringValue: [[textField stringValue] uppercaseString]];
If you want to limited the amount of digits, you can just use the deleteCharactersInRange: method of NSMutableString. If you're wanting to limit the kind of text you find in the text field, you can use an NSMutableCharacterSet with all of the characters that are allowed and then use NSScanner's scanCharactersFromSet:intoString method to filter out the unwanted characters.