( this is a technique I learned here: http://www.bit-101.com/blog/?p=1999 )
You could pass the method in the 'context', like
[theObject addObserver:self
forKeyPath:@"myKeyPath"
options:NSKeyValueObservingOptionNew
context:@selector(doSomething)];
..then in the observeValueForKeyPath method, you cast it to the SEL selector type, and then perform it.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
SEL selector = (SEL)context;
[self performSelector:selector];
}
If you want to pass data to the doSomething method you could use the 'new' key in the 'change' dictionary, like this:
[theObject addObserver:self
forKeyPath:@"myKeyPath"
options:NSKeyValueObservingOptionNew
context:@selector(doSomething:)]; // note the colon
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
SEL selector = (SEL)context;
// send the new value of observed keyPath to the method
[self performSelector:selector withObject:[change valueForKey:@"new"]];
}
-(void)doSomething:(NSString *)newString // assuming it's a string
{
label.text = newString;
}