改变窗口大小时的NSWindow事件

时间:2012-04-24 09:49:07

标签: objective-c

在Window中更改大小时的方法调用是什么? 我发现了大概windowDidResize:,所以我尝试了

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

我找到了需要使用NSWindowDidResizeNotification的内容,但我第一次使用NSNotification并且对此不太了解。 有人可以为我的活动写一个完整的例子吗?

2 个答案:

答案 0 :(得分:12)

在窗口委托上调用-windowDidResize:方法。是您发布窗口委托的方法的对象吗?

对于委托以外的其他人,你可以这样做:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:theWindow];

并且,当观察者不再感兴趣或被解除分配时:

[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:theWindow];

另一种方法是将新的基于块的API用于NSNotificationCenter

id observation = [[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidResizeNotification object:theWindow queue:nil usingBlock:^(NSNotification *){
    NSLog(@"test");
}];
// store/retain the observation for as long as you're interested in it. When it's deallocated, you stop observing.

答案 1 :(得分:0)

您可以实现NSWindowDelegate:

class YourVC: NSWindowDelegate {

    // This method trigger when you press the resize button in the window toolbar
    func windowDidResize(_ notification: Notification) {
   
          // Write your code here   

    }
}

然后,在viewDidLoad()或viewDidAppear()方法中

 self.view.window?.delegate = self

您还可以使用其他委托方法:

  • windowDidEnterFullScreen
  • windowDidExitFullScreen ...