如何以编程方式为 NSTabView 添加绑定?

发布于 2024-12-05 11:12:55 字数 941 浏览 0 评论 0原文

我的应用程序包含一个带有两个选项卡的 NSTabView。此外,应用程序本身有一个playState,它是一个枚举。 playState 保存在 Singleton 中。

typedef enum {
    kMyAppPlayStatePlaying,
    kMyAppPlayStatePaused
} MyAppPlayState;

playState 在此处合成。

@property (readwrite) MyAppPlayState playState;

我想在每次 playState 发生变化时切换 NSTabView 。因此,我准备了一个 IBOutlet 来添加与此类似的绑定。

[self.playPauseTabView bind:@"selectedItemIdentifier" toObject:[MyAppState sharedState] withKeyPath:@"playState" options:nil];

我已经认识到 identifier 必须是 NSString。这与我的 int 枚举不匹配。我也许可以使用 NSValueTransformer 来解决这个问题。
此外,selectedItemIdentifier 不存在。 NSTabView 仅提供selectedTabViewItem,然后允许访问identifierlabel。不过,我找不到根据标识符切换项目本身的方法。

My application contains an NSTabView with two tabs. Further, the application itself has a playState which is an enum. The playState is kept in a Singleton.

typedef enum {
    kMyAppPlayStatePlaying,
    kMyAppPlayStatePaused
} MyAppPlayState;

The playState gets synthesized here.

@property (readwrite) MyAppPlayState playState;

I want to switch the NSTabView every time the playState changes. Therefore, I prepared an IBOutlet to add a binding similar to this one.

[self.playPauseTabView bind:@"selectedItemIdentifier" toObject:[MyAppState sharedState] withKeyPath:@"playState" options:nil];

I already recognized the identifier must be NSString. This does not match with my enum which is an int. I could maybe use an NSValueTransformer to fix this.
Further, selectedItemIdentifier does not exists. NSTabView only offers selectedTabViewItem which then allows to access identifier or label. Though, I cannot find a way to switch the item itself based on the identifer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2024-12-12 11:12:55

在这种情况下,我发现自己正在做以下两件事之一:

1)将自身(或其他对象)注册为相关属性的观察者,并在 -observeValueForKeyPath:ofObject:change 中相应地设置所选选项卡:上下文:。它可能如下所示:

- (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 类型的属性,你可以绑定选项卡视图的选择。

In situations like that, I find myself doing one of two things:

1) Register self (or some other object) as an observer of the property in question, and set the selected tab accordingly in -observeValueForKeyPath:ofObject:change:context:. It could look like this:

- (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) declare a readonly NSString * property and declare that its value is affected by your playState property. Something like this:

@property (readonly) NSString *playStateStr;

// Accessor
-(NSString *)playStateStr
{
    return playState == kMyAppPlayStatePlaying ? @"playing" : "paused";
}

+(NSSet *)keyPathsForValuesAffectingPlayStateStr
{
    return [NSSet setWithObject: @"playState"];
}

Now you have an NSString-typed property that you can bind your tab view's selection.

你是我的挚爱i 2024-12-12 11:12:55

我忘记在 Interface Builder 中连接 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 Builder 中为每个选项卡设置它!

I forgot to connect the NSTabView with its IBOutlet in the Interface Builder.
The following works for me.

NSDictionary* playStateOptions = [NSDictionary dictionaryWithObject:[[PlayStateValueTransformer alloc] init] forKey:NSValueTransformerBindingOption];
[self.playPauseTabView bind:@"selectedLabel" toObject:[MyAppState sharedState] withKeyPath:@"playState" options:playStateOptions];

In the NSValueTransformer I return an NSString which must be set in Interface Builder for each tab!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文