Objective-C: Retrieving CGPoint from Key Value Observing - ios

I'm monitoring the center of a view with the key value observer pattern, as follows:
- (void)viewDidLoad {
[[self.balanceView viewWithTag:BASE_TAG] addObserver:self
forKeyPath:#"center"
options:0
context:BASE_CONTEXT];
}
I'm trying to retrieve the value of the centerpoint and output it to NSLog, as follows:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (BASE_CONTEXT == context) {
CGPoint baseCenter = [[change objectForKey:NSKeyValueChangeNewKey] CGPointValue];
NSLog(#"Base: (%f,%f)", baseCenter.x, baseCenter.y);
}
}
Whenever the UIView moves, an entry is output to NSLog. However, the centerpoint is always (0,0):
2014-07-18 20:48:51.210 Balance[8646:90b] Object: (0.000000,0.000000)
What is the correct way to retrieve the centerpoint?

options:0 gives you no change dictionary values -- if you want "new", you should use 1, or more appropriately, NSKeyValueObservingOptionNew.

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.

Observing NSProgress

I am using this code to track a changing number in NSProgress:
[progress addObserver:self forKeyPath:kProgressCancelledKeyPath options:NSKeyValueObservingOptionNew context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSProgress *progress = object;
NSLog(#"PROG: %f", progress.fractionCompleted);
// Check which KVO key change has fired
if ([keyPath isEqualToString:kProgressCancelledKeyPath]) {
// Notify the delegate that the progress was cancelled
}
else if ([keyPath isEqualToString:kProgressCompletedUnitCountKeyPath]) {
// Notify the delegate of our progress change
NSLog(#"PROG: %f", progress.fractionCompleted);
if (progress.completedUnitCount == progress.totalUnitCount) {
// Progress completed, notify delegate
NSLog(#"PROG: %f", progress.fractionCompleted);
}
}
}
This is what I get:
<NSProgress: phase=Loading; state=Waiting; fractionCompleted=0.000000> from peer: <MCPeerID: 0x14e1f220 DisplayName = alessandro's iPod touch-593288>
Noting more. I wanted to use a progress view with the fraction completed, but I don't get any value. The NSlog PROG is never called...

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:]

Detect Change in UILabel Text

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.

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