Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top