Bug 39413 - UIView AddObserver does not fire for Hidden property
Summary: UIView AddObserver does not fire for Hidden property
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.4 (iOS 9.2)
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Alex Soto [MSFT]
Depends on:
Reported: 2016-03-07 14:26 UTC by Tomasz Cielecki
Modified: 2016-03-08 11:26 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Swift playground (8.10 KB, application/zip)
2016-03-07 14:26 UTC, Tomasz Cielecki
XS project (11.66 KB, application/zip)
2016-03-07 16:06 UTC, Tomasz Cielecki
Swift playground 2 (8.04 KB, application/zip)
2016-03-07 16:29 UTC, Tomasz Cielecki

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Tomasz Cielecki 2016-03-07 14:26:41 UTC
Created attachment 15284 [details]
Swift playground

I am adding an observer either like:

    var observable = view.AddObserver(new NSString("hidden"), NSKeyValueObservingOptions.OldNew, OnHidden);


    view.AddObserver(this, new NSString("hidden"), NSKeyValueObservingOptions.OldNew, IntPtr.Zero);

in the latter case I have overridden the ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

Either observer does not fire when changing the Hidden property of the view is changed.

If I do the same in a Xcode Playground using Swift, the observer does get triggered. Why is there a difference between Swift and Xamarin?

I've also tried listening to isHidden and setHidden to no avail. It seems like the UIView implementation is missing WillChangeValue and DidChangeValue in its properties.
Comment 1 Tomasz Cielecki 2016-03-07 15:56:17 UTC
Hmm, after playing around with the same code in a new project I cannot reproduce. Still investigating.
Comment 2 Tomasz Cielecki 2016-03-07 16:06:31 UTC
Created attachment 15286 [details]
XS project

Xamarin.iOS project
Comment 3 Tomasz Cielecki 2016-03-07 16:12:36 UTC
I've attached a Xamarin.iOS project reproducing the problem.

It seems like having multiple degrees of nesting triggers this issue.

I.e. when having a hierarchy like this:

    |--> View1
          |--> View2
          |--> View3

When adding an observer in View1 for View2 and View3 for "hidden" ObserveValue won't get triggered. This is only if View2 and View3 are custom views like so:

public class View2 : UIView
	public View2(CGRect frame) : base(frame)

Explicitly overriding View2's Hidden property and calling WillChangeValue and DidChangeValue on hidden triggers the ObserveValue in View1.

How come it is necessary to explicitly implement KVO for properties you don't touch when inheriting from NSObjects?
Comment 4 Tomasz Cielecki 2016-03-07 16:29:21 UTC
Created attachment 15290 [details]
Swift playground 2

I've uploaded a new playground. It shows that in Swift nested Views even though not explicitly implementing KVO for hidden, still triggers the ObserveValue method. This is not the case in the Xamarin.iOS project.
Comment 5 Alex Soto [MSFT] 2016-03-08 05:14:49 UTC
Hello Tomasz! Glad to see you filling interesting bugs ;)

So I gave a try to your example and indeed one step further in the hierarchy does not work KVO'ing (new verb!) a UIKit object but I think it has something to do with the fact that UIKit framework does not support KVO as documentation states here[0] (not super clear but that gives us a hint)

> 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.

Fortunately in this case CALayer is KVO compliant as stated in this doc[1] so all you need (and should) do is kvo the layer property.

From your XS Project attachment you need to change the following lines from ViewController.cs:

Change Line 42 to: (just added `Layer` after `_hideShowView`)

> _hideShowView.Layer.AddObserver (this, "hidden", 
> NSKeyValueObservingOptions.OldNew, IntPtr.Zero);

Remove Line 46: (you should not call base here or an exception will be raised. System will think you did not manage your subscribed kVO's if you call base)

Once you do those two changes you should be fine and your sample should run as expected. Do not forget to remove your observers once you do not need them anymore in order to avoid random crashes.

Also do not forget you can use a much more delightful C# friendly Api[2]

Hope this helps!

[0]: https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/KVO.html#//apple_ref/doc/uid/TP40008195-CH16-SW1
[1]: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/Key-ValueCodingExtensions/Key-ValueCodingExtensions.html#//apple_ref/doc/uid/TP40004514-CH12-SW2
[2]: https://developer.xamarin.com/api/member/MonoTouch.Foundation.NSObject.AddObserver/p/System.String/MonoTouch.Foundation.NSKeyValueObservingOptions/System.Action{MonoTouch.Foundation.NSObservedChange}/
Comment 6 Tomasz Cielecki 2016-03-08 11:26:52 UTC
Using the Layer did indeed work.

For future reference, you can get hold of the UIView by looking at the WeakDelegate property of the Layer.