Objective c 更改窗口大小时的NSWindow事件

Objective c 更改窗口大小时的NSWindow事件,objective-c,Objective C,在窗口中更改大小时,方法调用是什么? 我发现了一些关于windowdidisize:的信息,所以我试着这样做 - (void)windowDidResize:(NSNotification *)notification { NSLog(@"test"); } 我找到了需要使用的NSWindowDidResizeNotification,但我第一次使用NSNotification,对此不太了解。 有人能为我的事件写一个完整的例子吗?在窗口委托上调用-windowdidisize:方法。具

在窗口中更改大小时,方法调用是什么? 我发现了一些关于
windowdidisize:
的信息,所以我试着这样做

- (void)windowDidResize:(NSNotification *)notification {
    NSLog(@"test");
}
我找到了需要使用的
NSWindowDidResizeNotification
,但我第一次使用NSNotification,对此不太了解。
有人能为我的事件写一个完整的例子吗?

在窗口委托上调用
-windowdidisize:
方法。具有您发布的方法的对象是否为该窗口的委托

对于委托以外的其他事项,您可以执行以下操作:

[[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.

您可以实现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()或ViewDidAspect()方法中

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

  • 视窗进入全屏
  • windowDidExitFullScreen

谢谢,但我不明白我必须用哪种方法写这篇文章
 self.view.window?.delegate = self