UITableViewController的视图作为子视图添加到UIViewController崩溃的应用程序

时间:2012-03-05 05:24:44

标签: iphone uitableview

我的项目中有这两个类:

#import <UIKit/UIKit.h>
#import "TableViewController.h"

@interface MainViewController : UIViewController
@end

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController
@end

在MainViewController.m中,我正在尝试这样做:

TableViewController *tview = [[TableViewController alloc] initWithNibName:@"TableViewController" 
                                    bundle:nil];  
tview.tableView.dataSource = tview;
tview.tableView.delegate = tview;
[self.view addSubview: tview.view];

然而,这是崩溃的:

-[MainViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xb52dc90'

我不明白为什么MainViewController成为TableViewController的数据源/委托,当我在上面的init中设置委托/数据源时。我还尝试从其viewDidLoad中将TableViewController的委托/数据源设置为self,但MainViewController仍然保持充当委托/数据源,无论如何。我尝试连接的nib文件并没有连接,但没有区别。

理想情况下,我希望TableViewController充当委托和数据源,并将其视图添加到MainViewController。我怎么能这样做?

谢谢!

注意:由于某种原因,这两种解决方案都不起作用。最后,我重新创建了TableViewController,继承自UIViewController并将其设置为,然后事情正常进行。

2 个答案:

答案 0 :(得分:0)

你在哪里写过TableView委托方法?它们应该在TableViewController类中,而不是在MainViewController类中。 在TableViewController类中编写UITableView委托方法。

希望这能解决你的问题...

答案 1 :(得分:0)

您是否在dealloc方法中将委托和数据源设置为nil?

tview.tableView.delegate = nil;
tview.tableView.dataSource = nil;
相关问题