防止 NSTextField 留空
我有一个 NSTextField ,里面有一个 NSNumberFormatter 。我见过一些文本字段,如果你将它们留空,它只会将之前的数字放回其中。我很好奇 Interface Builder 中是否有提供此行为的设置。我似乎找不到它,但我对 IB 相当陌生,可能没有找到正确的位置。
谢谢
I have a NSTextField with an NSNumberFormatter inside of it. I've seen textfields that if you leave them blank it just puts whatever number was in it previously back into it. I'm curious if there's a setting in Interface Builder that provides this behavior. I can't seem to find it, but I'm fairly new to IB and might not be looking in the right spot.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,IB 中除了默认值之外没有任何行为(这在这里没有帮助),但是您可以使用 NSTextFieldDelegate (NSControlTextEditingDelegate 的扩展)来监视编辑何时完成,使用 control:textShouldEndEditing: 如果将值留空,您可以将值放回框中。您可以在此处了解 NSTextFieldDelegate 。
There's no behaviour that I know of in IB other than the default value (which won't help here), but you could use NSTextFieldDelegate (extension of NSControlTextEditingDelegate) to monitor when editing finishes, using control:textShouldEndEditing: you can throw a value back into the box if it's left blank. You can read about NSTextFieldDelegate here.
如果您想保留一些默认值,以防用户删除输入
1) 子类 NSNumberFormatter
2) 实现(如果为空,将输入 0)
3) 在 IB 中设置类
If you want to leave just back some default value for case the user deleted the input
1) Subclass NSNumberFormatter
2) Implement (will put a 0, if empty)
3) set the class in IB