gpt4 book ai didi

cocoa - 如何使用 NSWindowDidExposeNotification

转载 作者:行者123 更新时间:2023-12-03 17:08:10 25 4
gpt4 key购买 nike

当另一个窗口变得可见时,我正在尝试更新另一个窗口。所以我找到了 NSWindowDidExposeNotification 并尝试使用它,所以我在我的 awakeFromNib 中写道:

// MyClass.m
- (void)awakeFromNib {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(mentionsWindowDidExpose:)
name:NSWindowDidExposeNotification
object:nil];
}

并实现了该方法

// MyClass.h
- (void)mentionsWindowDidExpose:(id)sender;

// MyClass.m
- (void)mentionsWindowDidExpose:(id)sender {
NSLog(@"test");
}

但它永远不会被调用,这很奇怪。我在这里做错了什么?

最佳答案

一般来说,您可以将 Controller 设置为窗口的 delegate为了接收这些通知,如下所示:

// MyClass.m
- (void)awakeFromNib {
// note: this step can also be done in IB by dragging a connection
// from the window's "delegate" property to your `MyClass` object
[window setDelegate:self];
}

- (void)windowDidExpose:(NSNotification *)notification {
NSLog(@"test");
}

虽然,读完here后和here , windowDidExpose 可能不是您最好的选择。我建议尝试 windowDidBecomeKey而是委托(delegate)方法。每当您的窗口获得“焦点”(开始响应用户输入)时就会发布该窗口,这可能是显示第二个窗口的正确时间。

更新:(回应评论)

Apple 的文档(下面引用)表明 NSWindowDidExposeNotification 仅对 nonretained 有效。 windows,根据我上面链接的帖子,这种情况相当罕见。

NSWindowDidExposeNotification

Posted whenever a portion of a nonretained NSWindow object is exposed, whether by being ordered in front of other windows or by other windows being removed from in front of it.

The notification object is the NSWindow object that has been exposed. The userInfo dictionary contains ... the rectangle that has been exposed.

在更高的层次上,NSNotification 对象只是在 Cocoa 类和 NSNotificationCenter 对象之间传递的数据包。 NSNotificationCenter 对象是管理这些数据包并根据需要将它们发送给观察者的 Controller 。通常不需要直接捕获通知。。您可以简单地在类中使用 KVC/KVO 或预定义的委托(delegate),Cocoa 会在幕后处理所有脏细节。

参见Notification Programming TopicsKey Value Coding Programming Guide如果您想了解更多。

关于cocoa - 如何使用 NSWindowDidExposeNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2723575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com