Robin Verdier
Robin Verdier

Reputation: 21

NSWindow resize notification

The second example in a course I'm preparing comparing three languages is handling a window resize event. It works trivially in the equivalents in Java and C#, but in the Mac Cocoa framework, the delegate handler for the NSWindow resize event never gets called. I added to the header file

- (void)windowDidResize: (NSNotification *)notification; 

and to the implementation file

- (void)windowDidResize: (NSNotification *)notification 
{
    NSString *name = notification.name;
    NSLog (@"Window was resized, notification %s", name);
}

When I resize the window, the message is never issued. What am I doing wrong? Mouse events work as they should. [MacBook Pro OSX 10.6.8, Xcode 3.2.6 64-bit].

Upvotes: 2

Views: 2447

Answers (2)

ingconti
ingconti

Reputation: 11666

my two cents for swift 5. (showing both delegate and notification, choose one..)

  import Cocoa

class ViewController: BaseController, NSWindowDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.makeItListenZoom()
    }



    override func viewDidAppear() {
        self.view.window?.delegate = self

    }


    private final func makeItListenZoom(){
        NotificationCenter.default.addObserver(forName: NSWindow.didResizeNotification, object: nil, queue: OperationQueue.main) { (n: Notification) in

            print("didresize---")
        }
    }



    func windowDidResize(_ notification: Notification){
           print("windowDidResize")
       }
}

Upvotes: 7

Daij-Djan
Daij-Djan

Reputation: 50129

either your obj needs to be the delegate or you have explicitly register witth the Notification Center to get the notifications

Upvotes: 1

Related Questions