首先,我必须在没有 IB 和 KVO 等高级 Objective-C 技术的情况下完成此操作。
我的问题来自一个简单的事实,即我无法找到获取文本字段的全新字符串值的方法。
我尝试使用委托(delegate)函数:
- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
但这并没有给我只提供替换字符串,而不是我验证所需的完整字符串。我稍后可以使用文本
我发现了
- (void)textDidChange:(NSNotification *)aNotification
- (void)controlTextDidChange:(NSNotification *)aNotification
但是当我接到这个电话时,已经太晚了,最后一个文本字段内容已经消失了。那么处理这个问题的最佳方法是什么?
是的,我确实读过 binding NSTextField to NSNumber但它不告诉我如何解决我的问题。
我需要的只是一个简单的“- (BOOL)acceptNewValue(NSString string)”测试函数。为什么 Cocoa 的一切都如此复杂,以至于开始感觉像 MFC。
最佳答案
I'm tried using the delegate function:
你的意思是实现该方法。使用它就意味着调用它,这是 NSTextView 的工作,而且它是一个 Objective-C 方法,而不是 C 函数。
- (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.
除非确实如此:您可以向 TextView 询问 the previous complete string , apply the change yourself到该字符串的可变副本,然后验证结果字符串。如果更改会导致无效输入,则返回 NO
。
关于cocoa - 验证 NSTextField 中的小数最大/最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3605438/