Detect Change in UILabel Text - ios

Is it possible to set a Notification for when a UILabel's text property is changed? I tried the one used for UITextFields when I couldn't find one for a UILabel, but it didn't work.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(posttosocial)
name:UITextFieldTextDidChangeNotification
object:nowplaying];

You can use key-value observing (KVO):
[label addObserver:self
forKeyPath:#"text"
options:NSKeyValueObservingOptionNew
| NSKeyValueObservingOptionOld
context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:#"text"]) {
/* etc. */
}
}

For Swift 3.2+
let observation = label.observe(\UILabel.text, options: [.new, .old]) { label, change in
print("\(change.newValue as? String ?? "" )")
}
Call observation.invalidate() when done observing.

Related

observer gets called without changing "playbackLikelyToKeepUp" iOS

I am writing a custom video player and here is my doubt:
Even though the value for the key "playbackLikelyToKeepUp" doesn't change,observer method is getting called.Here is the screenshot below:
How is this possible?
Here is the code:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if([keyPath isEqualToString:#"playbackLikelyToKeepUp"]){
NSLog(#"%#",change[#"new"]);
if([change valueForKey:#"new"] == [NSNumber numberWithBool:YES])
[myPlayer play];
NSLog(#"%#",change);
}
}
and in the viewDidLoad method:
[myPlayerItem addObserver:self forKeyPath:#"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
and you can see the screenshot above.

How to observe swipe event (cell position) of a UITableViewCell by Key-Value-Observer

I have a UITableView and custom UITableViewCell. When I swipe the cell in order to delete it, I have to exactly get informed about the position of the cell (view) in order to adjust its content accordingly. Therefore, I planned to use the key-value-observer (KVO) in the UITableViewCell class.
Add Observer
[self.contentView addObserver:self forKeyPath:#"center" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
Observer
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:#"center"]) {
NSLog(#"KVO: %#", change);
}
}
But I get no notification. What's wrong with my code?

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.

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