addObserver method works only within the same class - ios

I would like to add an observer on the property of an object.
I use this method :
[self addObserver:self forKeyPath:#"showButtons" options:NSKeyValueObservingOptionNew context:NULL];
When the observer and observed object is the same object the method observeValueForKeyPath i called, but when the observer is another object, the method is not called.
I explain what I have done.
I have an object ViewManager which has a property aString.
The class AppContainer has a property ViewManager.
In my class AppContainer, after the ViewManager property initilization, I add AppContainer as observer of ViewManager's aString property like this :
[self.viewManager addObserver:self forKeyPath:#"aString" options:NSKeyValueObservingOptionNew context:NULL];
In the AppContainer class, I have this method which is never called when the aString property changes.
Here is the never called method :
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(#"has changed");
}
However I have added an observer in the ViewManager class itself like this :
[self addObserver:self forKeyPath:#"aString" options:NSKeyValueObservingOptionNew context:NULL];
and the called method in this class is :
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(#"itself");
}
when the observer and abserved object are the same, it works.
Could I have some help ?

In the AppContainer class, I have this method which is never called when the aString property changes.
Maybe it's because you call self.viewManager addObserver when it's nil?

Related

Key-Value observing not working IOS

Hi I am developing IOS application in which I am using add observer with key value pair. But it's not working. I did following things:
[self.scrollView addObserver:self
forKeyPath:#"new"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
// inside here ...
}
But its not calling the above method. If I change forKeyPath to contentOffset then its working fine. But I want to change that value. Am I doing something wrong? Need Help. Thank you.
[webView.scrollView addObserver:self forKeyPath:#"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
You need to give the property of the scrollview here.

Is it possible to a property in a container view before awakeFromNib is called?

I have a container view that holds a view controller. I need to set a non-UI property in this view controller before awakeFromNib is called. However, the prepareForSegue method for the embed segue isn't called until after awakeFromNib happens.
Is there any way to pass this information to the contained view controller before awakeFromNib?
I have a similar issue in one of my apps.
Basically, I have a ViewController that has a property for the data model, but I am never sure when in my lifecycle the data model is actually set. My solution was to use Key-Value Observing to receive a callback when it's set.
Somewhere before the value can be set:
[self addObserver:self forKeyPath:#"propertyName" options: 0 context: nil];
Callback:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:#"propertyName"]) {
//do something
}
}
remember to unregister (I do it in my dealloc)
[self removeObserver:self forKeyPath:#"propertyName"];

Send Notification When a Property is Changed Using KVO

I had a property named myName in my class, like:
#property (nonatomic, strong) NSString *myName;
I need to send a notification when the myName property's value is changed.
Now I'm doing something like:
- (void)setMyName:(NSString *)name
{
_myName = name;
[[NSNotificationCenter defaultCenter] postNotificationName:CHANGE_NOTIFICATION object:nil];
}
I know there is something like Key-Value Observing in iOS. But I don't know how to implement it, I read the entire document, but couldn't get a good understanding.
Please help me to understand how to implement the same without using custom setter.
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
In - (void)setMyName:(NSString *)name do this instead
[self willChangeValueForKey:#"myName"];
_myName = name;
[self didChangeValueForKey:#"myName"];
//this generates the KVO's
And where you want to listen (the viewController), there in viewDidLoad add this line:
[w addObserver:self forKeyPath:#"myName"
options:NSKeyValueObservingOptionNew context:nil];
//By doing this, you register the viewController for listening to KVO.
and also implement this method:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([[change objectForKey:NSKeyValueChangeNewKey] isEqual:[NSNull null]]) {
return;
} else {
//read the change dictionary, and have fun :)
}
}
//this method is invoked, whenever the property's value is changed.
To do this without the customer setter, just synthesize the property setter. This will create all the supporting calls to willChangeValueForKey / didChangeValueForKey.
#synthesize myName;
Then set property values with dot-syntax:
self.myName = #"Inigo Montoya"
Then the observers will receive the KVO notification automatically.
(You will need to remove the observer before you release the observed object.)

Observing custom cell

I am trying to add observer (KVO) to observe my custom cell. Once the cell is selected I should receive a notification of the event.
My code:
[colMain addObserver:self forKeyPath:#"colMain" options:0 context:NULL];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (keyPath == #"colMain") {
NSLog(#"cell Selected");
[self performSelector:#selector(deleteCell) withObject:nil];
}
}
colMain stands for collectionView. I am not quite sure how to do it cause I don't have customCell as a property otherwise it does not compile.
Any ideas?
Why not just set a delegate on your collection view and then implement one of these two methods?
[– collectionView:shouldSelectItemAtIndexPath:]
[– collectionView:didSelectItemAtIndexPath:]

KVO on NSMutableString not working

I'm trying to observe changes to an NSMutableString isDetailView:
-(void)viewDidLoad {
[self addObserver:self forKeyPath:#"isDetailView" options:NSKeyValueObservingOptionNew context:nil];
[isDetailView setString:#"YES"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(#"obersedValueFOrKeyPath:%#", keyPath);
}
But the observeValueForKeyPath method never gets called. Any ideas?
You are not changing the property, only the content of the object it points to. If you make isDetailView a normal string and do
[self setIsDetailView: #"YES"]
it will work.
By the way, properties that start "is" are conventionally supposed to be boolean and that looks like a more appropriate type in this case too.

Resources