Send Notification When a Property is Changed Using KVO

后端 未结 3 785
独厮守ぢ
独厮守ぢ 2021-02-19 05:11

I had a property named myName in my class, like:

@property (nonatomic, strong) NSString *myName;

I need to send a notification whe

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-19 05:38

    Try this:

    MyClass *var = [MyClass new];
    [var addObserver:self forKeyPath:@"myName" options:NSKeyValueChangeOldKey context:nil];
    

    and implement

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
    
    }
    

    this method will be called anytime when myName property changes

提交回复
热议问题