将 NSSegmentedControl 绑定到布尔值?
我对 Cocoa Bindings 还很陌生,即使在浏览了文档之后我也没有找到这个问题的答案。我想要做的是有一个只有两个段的分段控件。如果选择第一个段,则 NSUserDefaults 中的首选项应为 YES ,但如果选择第二段,则首选项应为 NO 。通过代码来完成此操作很简单:
-(IBAction)segmentSelectionChanged:(id)sender {
NSInteger selectedSegment = [sender selectedSegment];
[[NSUserDefaults standardUserDefaults] setBool:(selectedSegment==0)?YES:NO forKey:@"somepref"];
}
但我想通过绑定来完成此操作(所选索引看起来很有希望)。有办法做这样的事情吗?谢谢!
I'm still new to Cocoa Bindings and I haven't found an answer to this question even after looking through the docs. What I want to do is have a segmented control that only has two segments. If the first segment is selected, then a preference in NSUserDefaults should be YES , but if the second segment is selected, then the preference should be NO. This is trivial to do through code:
-(IBAction)segmentSelectionChanged:(id)sender {
NSInteger selectedSegment = [sender selectedSegment];
[[NSUserDefaults standardUserDefaults] setBool:(selectedSegment==0)?YES:NO forKey:@"somepref"];
}
but I'd like to do it through bindings (selected index looks promising). Any way to do something like this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你已经明白了——在 IB: 中绑定控件的
selectedIndex
似乎工作得很好。
问题是您真的需要它是一个
BOOL
吗?无论如何,它只是signed char
的typedef
。请参阅 objc.h 第 43、49 和 50 行:您可以使用
integerForKey:
将值拉回并进行转换(可能更好,因为更明确):或者继续使用
boolForKey:
它应该可以正常工作。I think you've got it already -- binding the control's
selectedIndex
in IB:seems to work just fine.
Is the problem that you really need it to be a
BOOL
? It's just atypedef
forsigned char
anyways. See objc.h, lines 43, 49, and 50:You can pull the value back out using
integerForKey:
and cast it (possibly better because more explicit):or just continue using
boolForKey:
and it should work fine.