应用程序在处理导航栏button事件时崩溃

在我的应用程序中,我有一个导航栏button,允许用户弹回到导航堆栈的顶部。 这已经有一段时间了,一直工作正常。 但是,当我用MT 4.0构build它崩溃。

# in constructor UIButton btn = UIButton.FromType(UIButtonType.Custom); btn.Frame = new RectangleF(0,0,29,29); btn.SetBackgroundImage(UIImage.FromFile("images/Home_button.png"),UIControlState.Normal); btn.TouchDown += delegate { this.NavigationController.PopToRootViewController(true); }; UIBarButtonItem bar = new UIBarButtonItem(btn); this.NavigationItem.RightBarButtonItem = bar; 

该button绘制正确,但按下button时,应用程序崩溃。 如果我改变委托只是做一个Console.WriteLine()它仍然崩溃。

据我所知,这是工作,直到我安装MT 4.0。

它崩溃时显示的exception是

 Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26 at Application.Main (System.String[] args) [0x00000] in /Users/jason/Projects/myproj/myproj/AppDelegate.cs:44 

使用后退button进行导航工作正常。 这是一个错误,还是我做错了什么?

快速的问题,但我问这个,因为我多次对自己做了这个,对你的视图的引用,它是不是偶然地在你的主要方法的本地范围,是吗? 会发生什么是参考丢失,但屏幕仍然出现,但只要你想做的事情,它已被垃圾收集。

“后退”button由导航控制器本身所有,您可以通过该窗口引用该button,因此可以真正通过您。

就像快速提醒一样,当不是在局部variables上创buildbutton时,而是在包含button的视图上出现类似的崩溃。 我在这里描述了解决scheme。