处理导航栏按钮事件时应用程序崩溃 [英] app crashes when handling navbar button event

查看:23
本文介绍了处理导航栏按钮事件时应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个导航栏按钮,允许用户弹回导航堆栈的顶部.这已经存在了一段时间并且一直运行良好.但是,当我使用 MT 4.0 构建时,它崩溃了.

In my app, I have a navbar button that allows the user to pop back to the top of the navigation stack. This has been there for a while and has been working fine. However, when I build with MT 4.0 it crashes.

# 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;           

按钮绘制正确,但按下按钮时应用程序崩溃.如果我将委托更改为只执行 Console.WriteLine(),它仍然会崩溃.

The button is drawn correctly, but the app crashes when the button is pressed. If I change the delegate to just do a Console.WriteLine() it still crashes.

据我所知,在我安装 MT 4.0 之前,这一直有效.

As far as I can tell, this was working up until I installed MT 4.0.

它崩溃时显示的异常是

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

使用后退按钮导航工作正常.这是一个错误,还是我做错了什么?

Navigating using the back button is working normally. Is this a bug, or am I doing something wrong?

推荐答案

快速问题,但我问这个是因为我对自己做了几次,对您的视图的引用,它不在您的 Main 方法的本地范围内机会,是吗?发生的情况是引用丢失了,但屏幕仍然出现,但是一旦你想做某事,它就会被垃圾收集.

Quick question, but I ask this because I did this to myself several times, the reference to your View, it isn't locally scoped in your Main's method by chance, is it? What happens is that the reference is lost but the screen still appears but then as soon as you want to do something it's been garbage collected.

后退按钮归导航控制器本身所有,它可能会被您的窗口引用,因此它可以真正通过您关闭.

The Back button is owned by the navigation controller itself, which would have been referenced likely by your Window, and therefore it can really through you off.

这篇关于处理导航栏按钮事件时应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆