当用户聚焦/编辑 NSTextField 时,通过代码更改 NSTextField 的内容
我有一个 NSSlider 可以更新 NSTextField 的内容。到目前为止,一切都很好。只要 NSTextField 没有聚焦或被用户编辑(键入或保持空白等),它就可以工作。
我想要做的是通过代码强制更新 NSTextField,无论用户在做什么。
这是滑块更改值事件的代码:
-(IBAction) sliderTempoMoved: (id)sender{
[soundEngine setTempo:[sender floatValue]];
[labelTempo setStringValue:[NSString stringWithFormat:@"%0.2f%",[sender floatValue]]];
NSLog(@"slideTempoMoved: %f",[sender floatValue]);
}
只要 NSTextField 不被编辑,setStringValue 方法就可以工作...
有关此问题的任何线索吗?
问候, 亚历克斯
I have a NSSlider which updates the content of an NSTextField. So far so good. It works as long as the NSTextField isn't focused or gets edited by the user (typing or staying blank etc.).
What I'm trying to get working is to force the update of the NSTextField via code, no matter what the user is doing.
That's the code of the slider changed value event:
-(IBAction) sliderTempoMoved: (id)sender{
[soundEngine setTempo:[sender floatValue]];
[labelTempo setStringValue:[NSString stringWithFormat:@"%0.2f%",[sender floatValue]]];
NSLog(@"slideTempoMoved: %f",[sender floatValue]);
}
The setStringValue method works as long as the NSTextField doesn't get edited...
Any clues on this problem ??
Regards,
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这有多大帮助,但我尝试了您的代码,无论当前是否正在编辑 NSTextField 都会更新。检查你的 NSTextField 属性和我的属性。也许有什么东西阻止了它的更新。
您还可以尝试在调用
setStringValue
之前调用[labelTempo abortEditing]
。I don't know how much help this is, but I tried your code and the NSTextField gets updated whether or not it's currently being edited. Check your NSTextField properties against mine. Maybe something there is keeping it from being updated.
You could also try calling
[labelTempo abortEditing]
before the call tosetStringValue
.