Naren
Naren

Reputation: 724

Event becomes null in MonoTouch

Hi Monotouch users, I have this strange situation where I have a custom event declared in a custom UIView like so:

public delegate void SelectionChangedHandler(SelectableView selectedView, bool selected);
public event SelectionChangedHandler SelectionChanged;

I then assign to this handler in the initialize method of a viewcontroller like so:

SelectableView s;
...
SelectableView.SelectionChanged += HandleSelectionChanged;

I have checked and ensured that all the necessary objects still exist in memory and have not been garbage collected.

I am finding that the event becomes null sometime between the assignment and the drawing. Has anybody come across this issue before? Or am I doing something obviously wrong in my use of events in MonoTouch?

EDIT: Some additional information - the UIView is loaded from a xib file. I just tried with a UIView not loaded from a xib and everything works fine :| I am a little dumbfounded.

EDIT 2: Even more information: All the properties set on the UIView loaded from the xib appear to become null as well! Here is the code I use to load the UIView from the xib:

var v = new SelectableView(NSBundle.MainBundle.LoadNib("SelectableView", this, null).ValueAt(0));

I then set a property on v:

v.SomeProperty = s;

By render time SomeProperty is null.

Cheers Naren

Upvotes: 2

Views: 353

Answers (1)

jbehren
jbehren

Reputation: 770

I ran into a lot of issues trying to use Interface Builder created UIViews. I found two workarounds: 1) Use a UIViewController instead, which uses up quite a bit more memory, or 2) Create the UIView in code (no xib file), which is somewhat less comfortable than using IB.

I end up going with the first option most of the time - just be aware of the number of currently-loaded viewcontrollers.

Upvotes: 1

Related Questions