iOS 9: KVO no longer working - ios

I have an observer on a UITextView to detect if its content size is changing:
[_textView addObserver:self forKeyPath:#"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
This code always worked to call the following function, where I do resizing of the UITextView:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
However, in iOS 9, this function is never getting called. What changed in iOS 9 and how do I fix this?

If you do a search for UIKit and KVO Compliance you will see that everyone says you can't rely on it. See this question and this question.
I don't know what changed, but I think you should just subclass UITextView and overload setContentSize: if you want to know when it changes.

Related

KVO notifications with textView.text

I need notifications with textView.text, but the do not work.
My code:
Add a KVO:
[textView addObserver:self
forKeyPath:#"text"
options:NSKeyValueObservingOptionNew
context:nil];
Observing method
-(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
I am trying change contentSize. KVO is working...
https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/KVO.html
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.
There have another way to help you simply.
Conform UITextViewDelegate protocol methods.
Use method - (void)textViewDidChange:(UITextView *)textView; to get textView.text changed.

Detect when UIViewController is no longer presenting a view controller

I would like to observe a view controller that is presenting a view controller to determine when it stops presenting it.
I thought about trying KVO, which I'm not terribly familiar with:
[observedVC addObserver:self forKeyPath:NSStringFromSelector(#selector(presentedViewController)) options:0 context:NULL];
And then:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(#"Change: %#", keyPath);
}
But I am not seeing this get called. Is there a better way to go about this? I'm working on an SDK so I don't necessarily have any control over the observedVC.

Key-value observing doesn't work for zoomScale

I'm stuck with following problem. I have a UIScrollView, _myScrollView, and I want to have another UIScrollView following it's movements. So I'm using key-value observing for the properties "zoomScale" and "contentOffset", but the observeValueForKeyPath:ofObject:change:context: method only report changes in "contentOffset", not in "zoomScale", though the zooming workes fine. (See code snippet below.) What could be the reason for this?
-(void)viewDidLoad {
...
[_myScrollView addObserver:self forKeyPath:#"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
[_myScrollView addObserver:self forKeyPath:#"zoomScale" options:NSKeyValueObservingOptionNew context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:#"zoomScale"]) {
NSLog(#"zoomScale: %#", change); // Never gets called
}
...
}
The zoomScale property isn't KVO compliant. But UIScrollViewDelegate has a scrollViewDidZoom method that you can use to track changes to zoomScale.
UIKit actually doesn't support KVO.
From the docs:
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.
It does work sometimes (as you've seen), but support for it is undocumented and inconsistent. Use the delegate methods instead.

UITextView in UiCollectionView

I really need your help here. I have a UITextView in a UiCollectionViewCell. The problem i am facing here is when i select the cell to edit it , neither the textview methods nor the collectionview methods are called. For ex. neither the didSelectItemAtIndexPath nor the textViewDidBeginEditing methods are invoked.
As a result, I am not able to capture the edits made by the user the cell and save it .
Any inputs would be highly appreciated.
Thanks
I had faced similar problem in custom cell what i did was I had added following code to capture the events
[textField addObserver:self forKeyPath:#"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *tv = object;
//Your code using tv
}

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.

Resources