2014-03-05 44 views
2

我是新来的monotouch。我有一个UICollectionViewCell .xib文件,我有图像和标签设置。在另一个视图 - 控制它就像Monotouch UICollectionView给出空引用错误

public partial class TestViewController : UICollectionViewController { 

    public TestViewController (UICollectionViewLayout layout) : 
     base ("TestViewController", null) 
    { 
     CollectionView.RegisterNibForCell (UINib.FromName (
      "imagesCollectionViewCell", NSBundle.MainBundle), imageCellId); 
    } 
} 

它给在CollectionView.RegisterNibForCell (UINib.FromName ("imagesCollectionViewCell", NSBundle.MainBundle), imageCellId);

空引用异常请让我知道我做错了什么

回答

1

它看起来像[1] CollectionView属性null在此时间点 - 即在您的TestViewController类型的构造函数内。

它可能会在稍后(例如,当调用ViewDidAppear时)由iOS设置为有效(非空)值。在这种情况下,您应该在稍后执行时将您的呼叫转移到RegisterNibForCell

[1]您可以使用调试器查看任何字段/属性的值。

+0

如果我将RegisterNibForCell移动到ViewDidLoad并放置断点,它永远不会来ViewDidLoad – user3199271