Reputation: 730
I have a piece of code that runs perfectly until iOS 7, but now, with iOS 8 it does not work any more. I have a UILabel and I have a class that is set to observe any changes in its text. The setup is as below..
[lcdLabel addObserver:self
forKeyPath:@"text"
options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld
context:NULL];
And this is how I am registering the changes.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//other code related to observation goes here
}
Any help as to why this stopped working (the observeValueForKeyPath
method is not getting called any more) in iOS8?
Upvotes: 1
Views: 892
Reputation: 36
Same problem had here. I cannot change UILabel.text from delegate method. The method fires, but UILabel is not updated or screen refreshed... I am using SWIFT... tried to implement NSNotification centre- the same problem... Found out that I was calling delegate method from completion handler block and it was not able to update UIlabel as it has to be done on main thread.
Added:
dispatch_async(dispatch_get_main_queue(),{
... update label...
});
and worked like a charm
Upvotes: 0
Reputation: 2088
First of all, I have implemented a very basic key value observing (KVO) example on a UILabel's text property and it works consistently on both iOS 7 & iOS 8. So you may just have something wrong your code that you are not showing.
That being said, I do not think that KVO is the best approach to responding to changes of the lcdLabel.text property.
Apple's KVO document specifically states:
Note: Although the classes of the UIKit framework generally do not support KVO, you can still implement it in the custom objects of your application, including custom views.
Also, the documentation for UILabel mentions nothing of KVO compliance for the text property. Therefore, I do not think that you should rely on this approach.
Unlike a UITextField, the text property of a UILabel, will never change without you specifically setting it, you can simply subclass UILabel and override the setText method.
- (void)setText:(NSString *)text {
_text = text;
// other code related to observation goes here
// or call back to the UIViewController via a delegate through a custom protocol
}
You may also want to override setAttributedText as well.
Upvotes: 2