c# - 在 MonoTouch 中错误地加载 Nib 会导致内存泄漏吗?

标签 c# memory xamarin.ios xib

我正在使用 MonoTouch 创建我的第一个应用程序。这是一个相当简单的应用程序,它从 sqlite 数据库中搜索代码,并在不同的 View 中显示代码的各种含义。我使用过界面生成器。我的问题是关于加载 nib 文件的正确方法。

我的自定义 TableView Controller 是这样构造的:

public partial class HAZARDTableViewController : UITableViewController
{
    #region Constructors

    // The IntPtr and initWithCoder constructors are required for items that need 
    // to be able to be created from a xib rather than from managed code

    public HAZARDTableViewController (IntPtr handle) : base (handle)
    {
    }

    [Export ("initWithCoder:")]
    public HAZARDTableViewController (NSCoder coder) : base (coder)
    {
        Initialize ();
    }

    public HAZARDTableViewController ()
    {
        Console.WriteLine("HAZARDTableViewController():Constructor");
        Initialize ();
    }

在 .designer.cs 文件中:

public partial class HAZARDTableViewController : UITableViewController
{
#region Constructors

//The IntPtr and initWithCoder constructors are required for items that need 
// to be able to be created from a xib rather than from managed code

    public HAZARDTableViewController (IntPtr handle) : base (handle)
    {
    }

    [Export ("initWithCoder:")]
    public HAZARDTableViewController (NSCoder coder) : base (coder)
    {
        Initialize ();
    }

    public HAZARDTableViewController () //: base ("HAZARDTableViewController", null)
    {
        Console.WriteLine("HAZARDTableViewController():Constructor");
        Initialize ();
    }

    [MonoTouch.Foundation.Register("HAZARDTableViewController")]
    public partial class HAZARDTableViewController {

    private MonoTouch.UIKit.UIView __mt_view;

    private MonoTouch.UIKit.UITableView __mt_tableView;

    #pragma warning disable 0169
    [MonoTouch.Foundation.Connect("view")]
    private MonoTouch.UIKit.UIView view
    {
        get {
        this.__mt_view = ((MonoTouch.UIKit.UIView)(this.GetNativeField("view")));
        return this.__mt_view;
    }
    set {
        this.__mt_view = value;
        this.SetNativeField("view", value);
    }
    }

    [MonoTouch.Foundation.Connect("tableView")]
    private MonoTouch.UIKit.UITableView tableView {
        get {
            this.__mt_tableView = ((MonoTouch.UIKit.UITableView)(this.GetNativeField("tableView")));
        return this.__mt_tableView;
        }
    set {
            this.__mt_tableView = value;
            this.SetNativeField("tableView", value);
        }
    }

我关注的其中一件事是自定义 TableView Controller 如何加载 nib 文件。我的理解是应该使用这个构造函数:

public HAZARDTableViewController () : base ("HAZARDTableViewController", null)

这会导致此异常:

Unhandled Exception: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: -[UITableViewController loadView] loaded the "HAZARDTableViewController" nib but didn't get a UITableView.

我也按照 Bryan Costanich 在他的精彩教程中建议的那样尝试了这个构造函数来同步加载 Nib 。

MonoTouch.Foundation.NSBundle.MainBundle.LoadNib ("HAZARDTableViewController", this, null); ViewDidLoad();

当代码引用 ViewDidLoad 中的 TableView 时,这会导致空引用异常。 (顺便说一句,我尝试使用 tableView 而不是 TableView。)

我还根据 Xamarin 文档尝试了这个构造函数:

public HAZARDTableViewController (string nibName, NSBundle bundle) : base(nibName, bundle) {
    Initialize();
}

结果是:

Unhandled Exception: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: -[UITableViewController loadView] loaded the "HAZARDTableViewController" nib but didn't get a UITableView.

当我只创建自己的构造函数而不尝试加载我的 Nib 时,它会起作用。

public HAZARDTableViewController ()
{
    Console.WriteLine("HAZARDTableViewController():Constructor");
    Initialize ();
}

如果不专门加载 Nib ,我不明白它是如何工作的。我之所以担心,是因为我的应用程序在使用大约 6 或 7 分钟后就崩溃了。 我把它放在仪器上,它涌出内存。我已经清除了列表并搜索了松散的引用文献。无论如何,那是另一个问题。我正在仔细检查我的代码,我想知道我是否没有正确加载我的 nib 文件,是否有可能没有正确释放 nib 资源? IB 对我来说有点像黑盒子。

总而言之,我想知道: 1. Nib 的正确装填方式是什么? 2. 为什么以上尝试都不行? 3. 这会导致内存泄漏还是我找错地方了?

最佳答案

尝试更改您的基类。我还删除了 .xib 设计器文件。这是我的代码:

public partial class SearchResultsScreen : UITableViewController
{

    List<NewsArticle> NewsArticles;

    static NSString CellID = new NSString ("MyIdentifier");

    public SearchResultsScreen () : base(UITableViewStyle.Grouped)
    {
    }
}

关于c# - 在 MonoTouch 中错误地加载 Nib 会导致内存泄漏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8532944/

相关文章:

c# - .net 中的 java 的 ServerSocket

c++ - 如何减少内存使用 - 可能的内存泄漏

c++ - 使用 MAP_PRIVATE 与 MAP_SHARED 时 mmap 最坏情况的内存使用情况

ios - 如何将 NSAttributedString 绑定(bind)到 UITextView

c# - 如何在适用于 iOS 的 Xamarin Forms 中创建导航栏?

ios - UIView 背景上的图像纹理。多大算太大?

c# - MVVM 绑定(bind)在设计时不起作用

c# - 为什么要调用 Dispose()?不会发生内存泄漏?

java - 运行 Storm 拓扑时为 "java.lang.OutOfMemoryError: unable to create new native thread"

c# - 展开结果 View 将枚举 IEnumerable