Detect when UIViewController is no longer presenting a view controller - ios

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.

Related

iOS 9: KVO no longer working

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.

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.

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.

iOS: Key-Value Observing does not dismiss modal view

I have the following key value observer method in a modal view:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:#"uploadComplete"]) {
NSLog(#"UploadVC hears upload complete");
[self dismissViewControllerAnimated:YES completion:nil];
}
}
I use this to watch a photo object and know when it is finished uploading. When I run this it behaves as expected, and the console logs "UploadVC hears upload complete" - but then the following line is not executed -- the modal does not get dismissed.
There are no errors or anything else, the view just sits there and the modal is never dismissed. What's going on here?
That may happen when you receive KVO notification on background thread and so attempts to update UI may result in any unexpected behaviour (UI not changed, changed after some delay, app crash etc etc). Make sure you call all updating UI code on main thread:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:#"uploadComplete"]) {
NSLog(#"UploadVC hears upload complete");
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
}
}

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