LoginSignup
7

More than 5 years have passed since last update.

NSViewのサイズ変更通知

Last updated at Posted at 2013-01-31

NSViewのサブクラスでサイズ変更を検出するには、特定のメソッドをオーバーライドするのではなく、NSNotificationCenterを使う。もちろん、この方法ではサブクラスでなくても通知を受けることが出来る。
-[NSView initWithFrame:]などでNSNotificationCenterに登録する。

[[NSNotificationCenter defaultCenter] addObserverForName:NSViewFrameDidChangeNotification
                                                  object:self
                                                   queue:[NSOperationQueue mainQueue]
                                              usingBlock:^(NSNotification *note){ /* 処理 */ }];

Blocksを使わなければこう。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(hoge:)
                                             name:NSViewFrameDidChangeNotification
                                           object:self];

通知を受け取るメソッド(例ではhoge:)は第一引数にNSNotification*をとる必要がある。

NSOpenGLViewには-updateや-reshapeがあるので、NSViewにもそういうのがあるかと思っていた。
まぁ、もしそうならわざわざNSOpenGLViewで宣言したりしないよね。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7