如何以编程方式为 NSTabView 添加绑定?
我的应用程序包含一个带有两个选项卡的 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
,然后允许访问identifier
或label
。不过,我找不到根据标识符切换项目本身的方法。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,我发现自己正在做以下两件事之一:
1)将自身(或其他对象)注册为相关属性的观察者,并在
-observeValueForKeyPath:ofObject:change 中相应地设置所选选项卡:上下文:
。它可能如下所示:2) 声明一个只读 NSString * 属性,并声明其值受
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:2) declare a readonly NSString * property and declare that its value is affected by your
playState
property. Something like this:Now you have an NSString-typed property that you can bind your tab view's selection.
我忘记在 Interface Builder 中连接
NSTabView
及其IBOutlet
。以下内容对我有用。
在
NSValueTransformer
中,我返回一个NSString
,必须在 Interface Builder 中为每个选项卡设置它!I forgot to connect the
NSTabView
with itsIBOutlet
in the Interface Builder.The following works for me.
In the
NSValueTransformer
I return anNSString
which must be set in Interface Builder for each tab!