如何为NSApp.dockTile创建绑定 [英] How to create a binding for NSApp.dockTile's

查看:108
本文介绍了如何为NSApp.dockTile创建绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

NSDockTile(可通过[[NSApp dockTile] setBadgeLabel:@"123"]获得)没有出现在IB中,而且我无法弄清楚如何以编程方式绑定其"badgeLabel"属性,就像您可能绑定标签/文本字段一样/table列.

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.

有什么想法吗?

推荐答案

NSDockTile没有任何绑定,因此您的控制器将必须手动更新停靠图块.您可以使用KVO进行此操作,其效果与绑定它相同.

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.

创建一个全局上下文:


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

然后,在您的init方法中:

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.

这篇关于如何为NSApp.dockTile创建绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆