views:

16

answers:

1

First of all i have to do this without IB and without advanced Objective-C techniques like KVO.

My problem comes from the simple fact that i can't find a way to get the whole new string value of the text field.

I'm tried using the delegate function:

- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString

but this does not give me only the replacement string not the full string i need for validation. I can later use text

I found

- (void)textDidChange:(NSNotification *)aNotification 
- (void)controlTextDidChange:(NSNotification *)aNotification

but when i get this calls it is already to late and the last text field content is already gone. So what is the best way to handle this?

And yes i did read http://stackoverflow.com/questions/527322/binding-nstextfield-to-nsnumber but it gives me no clue how to solve my problem.

All i need is a simple "- (BOOL)acceptNewValue(NSString string)" testing function. Why is everything so complicated with Cocoa it starts to feel like MFC.

+2  A: 

I'm tried using the delegate function:

You mean implementing the method. Using it would mean calling it, which is NSTextView's job, and it's an Objective-C method, not a C function.

- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString

but this does not give me only the replacement string not the full string i need for validation.

Except it does: You can ask the text view for the previous complete string, apply the change yourself to a mutable copy of that string, and then validate the resulting string. Return NO if the change would result in invalid input.

Peter Hosey
Thanks this solves the problem. But i got a new onehttp://stackoverflow.com/questions/3605571/how-to-set-nsformatter-to-accept-negative-integer-values
Lothar