开发者

Validate decimal max/min values in a NSTextField

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)开发者_如何学C" testing function. Why is everything so complicated with Cocoa it starts to feel like MFC.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜