c# - UIViewController 子类(模仿 UITableViewController)未发布

标签 c# ios inheritance memory-management xamarin

我对 UIViewController 进行了子类化,它模仿了 UITableViewController == HUDTableViewController。然后我从这个子类 View Controller (SomeViewController : HUDTableViewController) 继承。

如果我模拟内存警告,SomeViewController 不会被释放。以下是 HUDTableViewController 的代码:

using System;

using Foundation;
using UIKit;

namespace MyApp
{
    public class HUDTableViewController : UIViewController, IUITableViewDataSource, IUITableViewDelegate, IDisposable, IUIScrollViewDelegate
    {
        private UIView parentView;
        private UITableView tableView;

        public UITableView TableView
        {
            get
            {
                return this.tableView;
            }
            set
            {
                this.tableView = value;
            }
        }

        public HUDTableViewController() : base()
        {
            Initialize();
        }

        private void Initialize()
        {
            this.tableView = new UITableView();
            this.tableView.TranslatesAutoresizingMaskIntoConstraints = false;

            this.tableView.WeakDelegate = this;
            this.tableView.WeakDataSource = this;

            this.parentView = new UIView();
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            this.parentView.AddSubview(this.tableView);
            View = this.parentView;

            NSMutableDictionary viewsDictionary = new NSMutableDictionary();
            viewsDictionary["parent"] = this.parentView;
            viewsDictionary["tableView"] = this.tableView;

            this.parentView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[tableView]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
            this.parentView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[tableView]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
        }

        [Foundation.Export("numberOfSectionsInTableView:")]
        public virtual System.nint NumberOfSections(UIKit.UITableView tableView)
        {
            return 1;
        }

        public virtual System.nint RowsInSection(UIKit.UITableView tableview, System.nint section)
        {
            throw new NotImplementedException();
        }

        public virtual UIKit.UITableViewCell GetCell(UIKit.UITableView tableView, Foundation.NSIndexPath indexPath)
        {
            throw new NotImplementedException();
        }

        [Export("tableView:estimatedHeightForRowAtIndexPath:")]
        public virtual System.nfloat EstimatedHeight(UIKit.UITableView tableView, Foundation.NSIndexPath indexPath)
        {
            return UITableView.AutomaticDimension;
        }

        [Foundation.Export("tableView:didSelectRowAtIndexPath:")]
        public virtual void RowSelected(UIKit.UITableView tableView, Foundation.NSIndexPath indexPath)
        {
        }

        [Export("tableView:heightForRowAtIndexPath:")]
        public virtual System.nfloat GetHeightForRow(UIKit.UITableView tableView, Foundation.NSIndexPath indexPath)
        {
            return 44.0f;
        }

        [Foundation.Export("tableView:heightForHeaderInSection:")]
        public virtual System.nfloat GetHeightForHeader(UIKit.UITableView tableView, System.nint section)
        {
            return UITableView.AutomaticDimension;
        }

        [Foundation.Export("tableView:viewForHeaderInSection:")]
        public virtual UIKit.UIView GetViewForHeader(UIKit.UITableView tableView, System.nint section)
        {
            return null;
        }

        [Export("tableView:titleForHeaderInSection:")]
        public virtual string TitleForHeader(UITableView tableView, nint section)
        {
            return string.Empty;
        }

        [Foundation.Export("tableView:willDisplayCell:forRowAtIndexPath:")]
        public virtual void WillDisplay(UIKit.UITableView tableView, UIKit.UITableViewCell cell, Foundation.NSIndexPath indexPath)
        {
        }
    }
}

tableView 的引用计数应该为 2(因为 AddSubView 和我的属性)。

这是主视图 Controller ,它实例化SomeViewController:

public class MasterViewContainer : UIViewController
{
    private bool hasSetupHandlersAndEvents = false;
    // ...

    public override void ViewWillAppear (bool animated)
    {
        base.ViewWillAppear (animated);

        if (!hasSetupHandlersAndEvents) {
            if (listButton != null) {
                listButton.Clicked += listButton_Clicked;
            }
            hasSetupHandlersAndEvents = true;
        }
    }

    public override void ViewWillDisappear (bool animated)
    {
        base.ViewWillDisappear (animated);

        if (hasSetupHandlersAndEvents) {
            if (listButton != null) {
                listButton.Clicked -= listButton_Clicked;
            }
            hasSetupHandlersAndEvents = false;
        }
    }

    private void listButton_Clicked(object sender, EventArgs args){
        SomeViewController viewController = new SomeViewController();
        viewController.SomeEvent += SomeEventHandler;
        NavigationController.PushViewController(viewController, false);
    }
}

如您所见,SomeViewController 引用了 MasterViewContainer,因为 SomeEventHandler

SomeViewController 如果我使用则释放

public class SomeViewController : UITableViewController

,但如果我使用它不会被释放

public class SomeViewController : HUDTableViewController

永远不会调用Dispose 方法。我没有看到引用周期。我必须在哪里发布一些东西?我缺少什么?

尝试 1:

这是我想到的唯一解决方案。我使用一个字段(类变量)来保存对 SomeViewController 的引用。在 DidReceiveMemoryWarning 中,我手动释放/处置它。当我想访问该字段时,我会检查它之前是否已初始化。如果不是,我会在需要时初始化它。

public class MasterViewContainer : UIViewController
{
    private SomeViewController viewController;

    public override void DidReceiveMemoryWarning ()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning ();

        // Release any cached data, images, etc that aren't in use.
        if (this.viewController != null)
        {
            this.viewController.SomeEvent -= SomeEventHandler;
            this.viewController.Dispose();
            this.viewController = null;
        }
    }

    private void listButton_Clicked(object sender, EventArgs args){
        if (this.viewController == null)
        {
            this.viewController = new SomeViewController();
            this.viewController.SomeEvent += SomeEventHandler;
        }

        NavigationController.PushViewController(this.viewController, false);
    }

但这个解决方案并不完美。当 View 当前在屏幕上时,也会调用处理。所以很可能出现故障。

赏金:

我想要一个解释内存管理问题的解决方案。为什么它没有被释放?必须改变什么才能发布它(没有像我尝试的那样做)。它的行为应该类似于 UITableViewController

尝试 2:

现在我尝试覆盖 HUDTableViewControllerDispose(bool disposing):

protected override void Dispose(bool disposing)
{
    if(!this.disposed)
    {
        if(disposing)
        {
           this.tableView.RemoveFromSuperview();
           this.tableView.Dispose();
        }
        this.disposed = true;
    }
    base.Dispose(disposing);
}

既没有调用 HUDTableViewControllerDispose 方法,也没有调用 SomeViewControllerDispose 方法。

最佳答案

如果您希望您的父 View 也调用相同的函数来处理您的管理,请调用 super。根据安排,您不需要进行任何其他手动处理。

public override void DidReceiveMemoryWarning ()
{
    // If you want the superclass to fire the function first call super first
    // and vice versa. 
    super.didReceiveMemoryWarning();
    // Releases the view if it doesn't have a superview.
    base.DidReceiveMemoryWarning ();

关于c# - UIViewController 子类(模仿 UITableViewController)未发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35019054/

相关文章:

c# - 下载图像时异步/等待死锁

iphone - 自定义 Facebook 好友选择器?

c# - 在 Unity 中检测 iPad 屏幕何时关闭或按下 Home 按钮的事件?

c# - 如何实现用更具体的类型覆盖基础成员的效果?

java - 复杂继承/包组合中的包私有(private)成员可见性 - Qiuck 验证可见性的一般规则

c# - 容器如何知道 child 何时调用了 InvalidateArrange?

C#:从后面的代码中拖动任何窗口(以编程方式)

c# - 对静态集合的线程安全访问

ios - 设置一个对象属性以匹配另一个

java - 为什么 Java 不允许扩展数组类型