在运行时实现委托?

我有一个通用的视图控制器类,我的应用程序中的所有视图控制器类inheritance其中有以下loadView方法:

 - (void) loadView { if (viewType == UIStandardViewControllerViewTypeUIView) { UIView *view = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]]; [self setView: view]; [view release]; } else if (viewType == UIStandardViewControllerViewTypeUIScrollView) { UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]]; [self setView: scrollView]; [scrollView release]; } else if (viewType == UIStandardViewControllerViewTypeUITableViewPlain || viewType == UIStandardViewControllerViewTypeUITableViewGrouped) { UITableViewStyle tableViewStyle; if (viewType == UIStandardViewControllerViewTypeUITableViewPlain) { tableViewStyle = UITableViewStylePlain; } else { tableViewStyle = UITableViewStyleGrouped; } UITableView *tableView = [[UITableView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame] style: tableViewStyle]; [self setView: tableView]; [tableView release]; } UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; [[self navigationItem] setBackBarButtonItem: backButton]; [backButton release]; } 

我这样做是因为很多原因,我不喜欢进入。 无论如何,你会注意到,要实现的视图types之一是tableview 。 众所周知, tableview需要一个delegate和一个datasource 。 我想知道是否有可能在运行时实现<UITableViewDelegate, UITableViewDataSource>当我知道一个tableView是做出的select?

如果没有,有没有其他人有任何想法,我可以做到这一点,而不必手动实现inheritance视图控制器类中的委托和数据源? 如果我在编译时(通常)在我的UIStandardViewController类中实现数据源和委托,那么我会得到警告,因为我需要在标准视图控制器类中实现强制数据源和委托方法。 你会实现这些,然后在子类中重写它们吗? 或者任何人有任何想法,我怎么可以做到这一点干净?

更新:想知道,如果我刚刚在我的UIStandardViewController类中实现了委托和数据源,并且还实现了所需方法的空白版本,那么当我不使用tableview时,这会带来很多额外的开销吗?

你可以写一个控制器(只是控制器,而不是视图控制器),实现数据源和表视图委托。 你只会在需要的时候创build一个实例。

另请注意,您正在使用工厂模式。 你应该使用一个类方法来创build新的视图。 签名会像+(UIView *)viewWithType:(ViewTypeStyle) viewTypeSyle)

TableController.h

 #import <Foundation/Foundation.h> @interface TableController : NSObject <UITableViewDataSource,UITableViewDelegate> @end 

TableController.m

 #import "TableController.h" @implementation TableController -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 100; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MyCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; } cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row]; return cell; } @end 

ViewController.m

 #import "ViewController.h" #import "TableController.h" @interface ViewController () @property(nonatomic,retain) UITableView *tableView; @property(nonatomic,retain) TableController *controller; @end @implementation ViewController @synthesize tableView = tableView_; @synthesize controller = controller_; -(void)dealloc { self.tableView = nil; self.controller= nil; [super dealloc]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.controller = [[[TableController alloc] init] autorelease]; self.tableView = [[[UITableView alloc] initWithFrame:self.view.frame] autorelease]; self.tableView.delegate = self.controller; self.tableView.dataSource= self.controller; [self.view addSubview:self.tableView]; // Do any additional setup after loading the view, typically from a nib. } //... @end