gpt4 book ai didi

objective-c - 将 NSSegmentedControl 绑定(bind)到 bool 值?

转载 作者:行者123 更新时间:2023-12-03 18:01:28 24 4
gpt4 key购买 nike

我对 Cocoa Bindings 还很陌生,即使在浏览了文档之后我也没有找到这个问题的答案。我想要做的是有一个只有两个段的分段控件。如果选择第一个段,则 NSUserDefaults 中的首选项应为 YES ,但如果选择第二段,则首选项应为 NO 。通过代码可以轻松完成此操作:

-(IBAction)segmentSelectionChanged:(id)sender {
NSInteger selectedSegment = [sender selectedSegment];
[[NSUserDefaults standardUserDefaults] setBool:(selectedSegment==0)?YES:NO forKey:@"somepref"];
}

但我想通过绑定(bind)来完成(所选索引看起来很有希望)。有办法做这样的事情吗?谢谢!

最佳答案

我想你已经明白了——在 IB 中绑定(bind)控件的 selectedIndex:

Bind To: Shared User Defaults ControllerController Key: valuesKey Path: WhateverYouWant

seems to work just fine.

Is the problem that you really need it to be a BOOL? It's just a typedef for signed char anyways. See objc.h, lines 43, 49, and 50:

typedef signed char     BOOL;
// ...
#define YES (BOOL)1
#define NO (BOOL)0

您可以使用integerForKey:将值拉出来并进行强制转换(可能更好,因为更明确):

(BOOL)[[NSUserDefaults sharedUserDefaults] integerForKey:@"WhateverYouWant"];

或者继续使用boolForKey:,它应该可以正常工作。

关于objective-c - 将 NSSegmentedControl 绑定(bind)到 bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6052869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com