What is the proper way of changing current UIViewController without a memory leak in MonoTouch?
I have a base UINavigationController class ( xib-less code ) using the singleton pattern with the fallowing code
[MonoTouch.Foundation.Register("NavController")]
public class NavController : UINavigationController
{
public static NavController Instance = new NavController();
public void ChangePage( UIViewController sender, UIViewController page, double duration )
{
this.PopViewControllerAnimated(true);
this.PushViewController( page, false );
//
SetViewControllers( new UIViewController[]{ page }, false );
if( sender != null )
{
sender.View.RemoveFromSuperview();
sender = null;
}
GC.Collect();
}
}
And also i have 4 UIWebController classes with this structure
[Register("HomePage")]
public class AViewPage : UIViewController
{
UIButton btn = new UIButton( new RectangleF( 0,0, 100, 100) );
UIImageView img = new UIImageView( new RectangleF( 100, 100, 200, 200 );
//and a lot of other widgets
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
//
btn.TouchUpInside += delegate{
AnotherViewPage page = new AnotherViewPage();
NavController.Instance.ChangePage( this, page, 1 );
}
//
this.View.AddSubview( btn );
this.View.AddSubview( img );
}
}
My problem is that with current code, memory is leaking. What am I doing wrong?
- Should i override the Dispose(bool) and manually dispose everything on the UIWebController classes ?
- I also have a lot of UIImages and UIImageViews on this UIViewControllers, what is the proper way of disposing them ?
- I have the feeling that the ChangePage function code i开发者_运维问答s ugly. Help me with a better example please.
A fix is coming in the MonoTouch 4.1 beta release, a temporary workaround is this:
Replace this line:
sender.View.RemoveFromSuperview();
With:
var super = sender.View.Superview;
sender.View.RemoveFromSuperview ();
var scratch = super.Subviews;
The correct fix in the next release will just work. Apologies for that.
Ok, stupid question from the beginning.
If you run in to the same problem, just read a little about PushViewController, PopViewControllerAnimated, ViewDidAppear, ViewDidDisappear and ViewDidLoad.
精彩评论