我已经在我的项目,这两个类:的UITableViewController的观点添加子视图的UIViewController崩溃的应用程序
#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的委托/数据源自我,但MainViewController仍保持作为委托/数据源,不管。我尝试着将nib文件连接起来,而不是连接起来,但没有任何区别。
理想情况下,我想TableViewController充当委托和数据源,并有其视图添加到MainViewController。我怎样才能做到这一点?
谢谢!
注:既不解决方案工作因为某种原因。最后,我重新创建了TableViewController作为从UIViewController继承,并将其设置为,然后事情正常进行。
委托和数据源方法写在TableViewController类中。 – kurisukun 2012-03-05 05:44:49
你只要移除'tview.tableView.delegate = tview; tview.tableView.dataSource = tview;'因为TableViewController是一个UITableViewController类,所以你不需要明确提到委托类。 – 2012-03-05 06:00:45