Pavel
Pavel

Reputation: 413

NSWindow event when change size of window

what is method call when change size in Window? I find somesing aboud windowDidResize: so i try doing

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

I found what need use NSWindowDidResizeNotification, but I work for the first time with NSNotification and bad understand about this. Can somebody write a full example for my event, please?

Upvotes: 4

Views: 7624

Answers (2)

M Afham
M Afham

Reputation: 960

You can Implement 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   

    }
}

And, In viewDidLoad() or viewDidAppear() method

 self.view.window?.delegate = self

You can also use other delegate methods:

  • windowDidEnterFullScreen
  • windowDidExitFullScreen ...

Upvotes: 0

Ken Thomases
Ken Thomases

Reputation: 90551

The -windowDidResize: method is called on the window delegate. Is the object with the method you posted the delegate for the window?

For something other than the delegate, you can do:

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

and, when the observer is no longer interested or being deallocated:

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

Another approach is to use the new block-based API to 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.

Upvotes: 12

Related Questions