我正在使用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.这可能是造成内存泄漏还是我看错了地方?