Archive for August, 2010

Var, MonoTouch & SDK4.0

August 3, 2010

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