Var, MonoTouch & SDK4.0

Since upgrading to iPhone SDK4.0 I have seen a difference in MonoTouch using the ‘var’ type, when used in nested event handlers.

For example in the code below, I get a crash when I hit the OK button. This never happened in the previous MonoTouch|iPhoneSDK incarnations.

 
public partial class FlipsideViewController : UIViewController {
 public override void ViewDidLoad () {
  base.ViewDidLoad ();
  var right = new UIBarButtonItem("Hello", UIBarButtonItemStyle.Bordered, null);
  right.Clicked += HandleButtonRightClicked;
  _navigationItem.RightBarButtonItem = right;
 }

 void HandleButtonRightClicked (object sender, EventArgs e) {
  var alert = new UIAlertView("Message","This is a message",null, "OK", null);
  alert.Dismissed += delegate { };
  alert.Show();
 }
}

Obviously this is how you can fix it,
private UIAlertView alert = null;
void HandleButtonRightClicked (object sender, EventArgs e) { alert = new UIAlertView("Message","This is a message",null, "OK", null); alert.Dismissed += delegate { }; alert.Show(); }
BTW, the exception related an unhandled exception "unrecognized selector sent to instance".

Advertisements

2 Responses to “Var, MonoTouch & SDK4.0”

  1. ChrisNTR Says:

    hey mtrinder.

    This is because MonoTouch was updated with a better, more strict garbage collector. In MonoTouch 3.0.12 – UIAlertView and UIActionSheets keep reference now – see http://monotouch.net/Releases/MonoTouch_3/MonoTouch_3.0.12

    Hope this helps,

    ChrisNTR

    • mtrinder Says:

      Yeah I figured it was GC. Someone on Twitter (from MonoTouch forum) was seeing the “unrecognized selector sent” exception, and that is the only time I’ve seen it recently. Cheers very much for the info & link. Mark.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: