iOSアプリ開発のメモ置き場

ささたつがiOSアプリ開発で知ったObjective-Cのtipsなどを書いていく所存

プロパティが変更したらそれをKVOで検知する方法

監視オブジェクトの追加

[self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:NULL];


監視オブジェクトの受信

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"keyPath=%@\nobject=%@\nchange=%@\ncontext=%@", keyPath, object, change, context);
}


こんな感じで出力される。便利〜。

keyPath=frame
object=<LQAInquiryButtonView: 0x11931cd80; frame = (0 0; 320 94); autoresize = W+H; layer = <CALayer: 0x11935f270>>
change={
    kind = 1;
    new = "NSRect: {{0, 0}, {320, 94}}";
    old = "NSRect: {{0, 0}, {320, 50}}";
}
context=(null)


参考:Key-value observingを使う際のtips