如何为 NSApp.dockTile 创建绑定

发布于 2024-07-10 12:31:57 字数 191 浏览 12 评论 0原文

在 IB 中,很容易将标签或文本字段绑定到某些控制器的 keyPath。

NSDockTile(可通过 [[NSApp dockTile] setBadgeLabel:@"123"] 获得)不会出现在 IB 中,并且我无法弄清楚如何以编程方式绑定其“badgeLabel”属性,就像绑定标签/文本字段/表列一样。

有任何想法吗?

In IB it is easy to bind a label or text field to some controller's keyPath.

The NSDockTile (available via [[NSApp dockTile] setBadgeLabel:@"123"]) doesn't appear in IB, and I cannot figure out how to programmatically bind its "badgeLabel" property like you might bind a label/textfield/table column.

Any ideas?

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

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

发布评论

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

评论(3

つ低調成傷 2024-07-17 12:31:57

NSDockTile 没有任何绑定,因此您的控制器必须手动更新停靠图块。 您可以使用 KVO 来完成此操作,这与绑定它具有相同的效果。

创建一个全局上下文:


static void* MyContext=(void*)@"MyContext";

然后,在 init 方法中:


[objectYouWantToWatch addObserver:self forKeyPath:@"dockTileNumber" options:0 context:MyContext];

然后,您必须实现此方法才能收到关键路径更改的通知:

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == MyContext) {
        [[NSApp dockTile] setBadgeLabel:[object valueForKeyPath:keyPath]];
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

确保在控制器对象消失时删除观察者。

NSDockTile doesn't have any bindings, so your controller will have to update the dock tile manually. You could do this using KVO which would have the same effect as binding it.

Create a context as a global:


static void* MyContext=(void*)@"MyContext";

Then, in your init method:


[objectYouWantToWatch addObserver:self forKeyPath:@"dockTileNumber" options:0 context:MyContext];

You then have to implement this method to be notified of changes to the key path:

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == MyContext) {
        [[NSApp dockTile] setBadgeLabel:[object valueForKeyPath:keyPath]];
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

Make sure you remove the observer when the controller object goes away.

維他命╮ 2024-07-17 12:31:57

如果 NSDockTile 确实支持绑定,则可以使用方法 bind:toObject:withKeyPath:options: 在 badLabel 属性上设置绑定。 检查文档以获取有关使用哪些参数的详细信息。 如果它不起作用,您可以在控制器类中实现键值观察并在每次值更改时更新标签,甚至重写 NSDockTile 以创建绑定兼容的子类。

If NSDockTile does support bindings, you can use the method bind:toObject:withKeyPath:options: to set up bindings on the badgeLabel property. Check the documentation for details on which arguments to use. If it doesn't work, you could either implement key value observing in your controller class and update the label each time the value changes, or even override NSDockTile to create a bindings compatible subclass.

转身泪倾城 2024-07-17 12:31:57

我在 NSDockTile、控制器、数据源上尝试了 bind:toObject:withKeyPath:options: 的许多变体。 我无法找出有效的组合。 或者,是否有一种方法可以将 BatchController 对象绑定到数据源,然后更新徽章? 如何获取 NSObject 并使其可绑定?

I've tried lots of variations of bind:toObject:withKeyPath:options: on NSDockTile, on a controller, on the data source. I can't figure out a combination that works. Alternately, is there a way of having a BatchController object that can be bound to the data source, and it then updates the badge? How do I take an NSObject and make it bindable?

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