4

在Window中更改大小时的方法调用是什么?我发现一些事情windowDidResize:,所以我尝试做

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

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

4

2 回答 2

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.
于 2012-04-24T10:19:26.950 回答
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 ...
于 2020-12-04T09:27:54.667 回答