我的应用程序包含一个带有两个选项卡的NSTabView。此外,应用程序本身有一个playState,它是一个枚举。playState保存在一个单例中。
typedef enum {
kMyAppPlayStatePlaying,
kMyAppPlayStatePaused
} MyAppPlayState;在这里合成了playState。
@property (readwrite) MyAppPlayState playState;每当NSTabView发生变化时,我都想切换playState。因此,我准备了一个IBOutlet来添加与此绑定类似的绑定。
[self.playPauseTabView bind:@"selectedItemIdentifier" toObject:[MyAppState sharedState] withKeyPath:@"playState" options:nil];我已经认识到identifier一定是NSString。这与我的枚举( int )不匹配。我也许可以用一个NSValueTransformer来解决这个问题。
此外,selectedItemIdentifier不存在。NSTabView只提供selectedTabViewItem,然后允许访问identifier或label。不过,我无法找到一种基于标识符切换项目本身的方法。
发布于 2011-09-19 15:32:52
在这种情况下,我发现自己做了两件事之一:
1)将self (或其他一些对象)注册为有关属性的观察者,并在-observeValueForKeyPath:ofObject:change:context:中相应地设置所选选项卡。看起来可能是这样的:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ( context == PlayStateChange )
{
if ( [[change objectForKey: NSKeyValueChangeKindKey] integerValue] == NSKeyValueChangeSetting )
{
NSNumber *oldValue = [change objectForKey: NSKeyValueChangeOldKey];
NSNumber *newValue = [change objectForKey: NSKeyValueChangeNewKey];
NSInteger oldInteger = [oldValue integerValue];
NSInteger newInteger = [newValue integerValue];
NSLog(@"Old play state: %ld, new play state: %ld", (long)oldInteger, (long)newInteger);
// Do something useful with the integers here
}
return;
}
}2)声明只读NSString *属性,并声明其值受playState属性的影响。就像这样:
@property (readonly) NSString *playStateStr;
// Accessor
-(NSString *)playStateStr
{
return playState == kMyAppPlayStatePlaying ? @"playing" : "paused";
}
+(NSSet *)keyPathsForValuesAffectingPlayStateStr
{
return [NSSet setWithObject: @"playState"];
}现在您有了一个NSString类型的属性,可以绑定选项卡视图的选择。
发布于 2011-09-19 15:30:38
我忘了在Interface中将NSTabView与其IBOutlet连接起来。
下面这些对我来说很有用。
NSDictionary* playStateOptions = [NSDictionary dictionaryWithObject:[[PlayStateValueTransformer alloc] init] forKey:NSValueTransformerBindingOption];
[self.playPauseTabView bind:@"selectedLabel" toObject:[MyAppState sharedState] withKeyPath:@"playState" options:playStateOptions];在NSValueTransformer中,我返回一个NSString,它必须在Interface中为每个选项卡设置!
https://stackoverflow.com/questions/7472412
复制相似问题