我正在使用MonoTouch創建我的第一個應用程序。這是一個相當簡單的應用程序,它從sqlite數據庫搜索代碼,併爲不同視圖上的代碼提供各種含義。我用過Interface Builder。我的問題是關於加載nib文件的正確方法。可能在MonoTouch中加載不正確的筆尖會導致內存泄漏?
我的自定義表視圖控制器構造爲這樣:
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);
}
}
一個我在看的東西是如何自定義表視圖控制器加載nib文件。我的理解是,此構造應使用:
public HAZARDTableViewController() : base ("HAZARDTableViewController", null)
這導致了這個異常:
未處理的異常:MonoTouch.Foundation.MonoTouchException:Objective-C中的異常。名稱:NSInternalInconsistencyException原因: - [UITableViewController loadView]加載了「HAZARDTableViewController」筆尖,但沒有得到UITableView。
我也嘗試過這個構造函數,正如Bryan Costanich在他的教程中提出的一樣,可以同步加載筆尖。
MonoTouch.Foundation.NSBundle.MainBundle.LoadNib ("HAZARDTableViewController", this, null); ViewDidLoad();
這導致空引用異常時的代碼是指ViewDidLoad
的TableView
。 (順便說一句我一直在使用的tableView
代替TableView
嘗試。)
我也試着根據Xamarin documenation這個構造:
public HAZARDTableViewController (string nibName, NSBundle bundle) : base(nibName, bundle) {
Initialize();
}
導致:
未處理的異常:MonoTouch的。 Foundation.MonoTouchException:拋出了Objective-C異常。名稱:NSInternalInconsistencyException原因: - [UITableViewController loadView]加載了「HAZARDTableViewController」筆尖,但沒有得到UITableView。
它工作時,我只是讓自己的構造函數沒有試圖加載我的筆尖。
public HAZARDTableViewController()
{
Console.WriteLine("HAZARDTableViewController():Constructor");
Initialize();
}
我不明白這是如何工作,沒有專門加載筆尖。爲什麼這個問題是我的應用程序在大約6或7分鐘的使用後纔會崩潰。 我已經通過儀器,它涌出內存。我清除了列表並搜索了散列引用。無論如何,這是另一個問題。我正在仔細檢查我的代碼,我在想我是否沒有正確加載我的nib文件,有可能它沒有正確地釋放nib資源? IB對我來說有點黑盒子。
所以總結一下,我想知道: 1.什麼是加載筆尖的正確方法? 2.爲什麼上述嘗試不起作用? 3.這可能是造成內存泄漏還是我看錯了地方?