开发者

MonoTouch: UITableView outlet variable is null

I am running through the "Navigating with tables" section of Wrox' Profes开发者_开发百科sional iPhone Programming with MonoTouch by McClure et al, picking up the basics of putting together a hierarchical UI for iOS, and running into the following problem.

I have created a new "iPhone View with Controller" file (called ParametersViewController), deleted the UIView from it, added a UITableView, created an outlet for it (tableView) and connected the "File's Owner" view outlet to the UITableView, per the tutorial.

In the RowSelected method of this view's parent view, I instantiate my ParametersViewController, calling the default constructor, in which I want to set up the table view's data source:

this.tableView.Source = new DataSource(this, new [] {"one", "two", "three"});

(DataSource is a nested class which inherits from UITableViewSource)

All compiles and runs fine, up until the point where that line is executed. Turns out that this.tableView is null, so I get a NullReferenceException.

tableView is the outlet for the table. How can it be null? Can't I set up the table source here in the constructor? If not, where do I do it?


Solution to this was

  • Don't create an outlet called tableView
  • Make the view a subclass of UITableViewController, instead of UIViewController
  • Refer to the base.TableView property of my UITableViewController, rather than the nonsense outlet I created


I'm not 100% clear on what you're doing where, but it might be that you need to access your UITableView in the UIViewController's ViewDidLoad method. At that point your NIB view(s) should be instantiated.

If that's not the problem, it may help if you could provide some more code so that we can see exactly what's going on. I think the lack of responses might due to people not being sure that they understand the nature of your problem exactly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜