MonoTouch.Dialog crash

本小妞迷上赌 提交于 2019-12-02 05:17:08

Avoid patterns like:

var navigationController = new UINavigationController(dialogViewController);
View.Add (navigationController.View);

because navigationController won't be referenced (on the managed side) once the View.Add call is done and the garbage collector can dispose it (whenever it wants). However from the native side it will be expected to exist. Calls going back (from native to managed) to the disposed instance will crash your application.

The right pattern is to declare navigationController as a field (instead of a local variable) of your type and create/assign it in the method. This will keep a reference to navigationController alive as long as the parent instance exists (and ensure any callback won't go to a disposed object).

E.g.

private UINavigationController navigationController;
...
public override void ViewDidLoad ()
{
    ...
    var dialogViewController = new DialogViewController(rootElement);
    navigationController = new UINavigationController(dialogViewController);

    View.Add (navigationController.View);
    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!