在运行时实现代理? [英] Implement Delegate at Run Time?

查看:94
本文介绍了在运行时实现代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a universal view controller class which all of the view controller classes in my app inherit from which has the following loadView method:

- (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];
}



我这样做了很多原因,我不感觉进入。无论如何,你会注意到,要实现的视图类型之一是 tableview 。我们都知道, tableview 需要一个委托和一个数据源。我想知道是否可以在运行时实现< UITableViewDelegate,UITableViewDataSource> 当我知道一个tableView是做出的选择?

I have it done this way for a lot of reasons that I don't feel like getting into. Anyhow, as you'll notice, one of the view types to implement is a tableview. As we all know, a tableview needs a delegate and a datasource. I was wondering if it's possible to implement the <UITableViewDelegate, UITableViewDataSource> at run time when I know a tableView is the choice that was made?

如果没有,其他人有什么想法我怎么能这样做而不必手动实现我的委托和datasource在继承的视图控制器类?如果我在编译时(通常)在我的 UIStandardViewController 类中实现数据源和委托,那么我得到警告,因为我需要实现强制数据源和委托方法标准视图控制器类。你会实现这些,然后在子类中覆盖它们吗?

If not, does anyone else have any ideas how I can do this without having to manually implement my delegate and datasource in the inheriting view controller class? If I implement the data source and delegate at compile time (normally) in my UIStandardViewController class, then I get warning because I need to implement the mandatory data source and delegate methods in my standard view controller class. Would you implement these and then just override them in child class? Or anyone have any idea of how I can do this cleanly?

更新:想知道,如果我只是在我的UIStandardViewController类中实现了委托和数据源,并且也实现了

UPDATE: Was wondering, if I just implemented the delegate and data source in my UIStandardViewController class, and also implemented empty versions of the required methods, would this be a lot of extra overhead when I didn't use a tableview?

推荐答案

您需要的方法的空版本,这将是很多额外的开销可以编写一个实现数据源和表视图委托的控制器(只是控制器,而不是视图控制器)。如果需要,您将只创建一个实例。

You can write a controller (just controller, not view controller) that implements data source and the table view delegate. You will only create an instance if needed.

还要注意,您使用的是工厂模式。您应该使用类方法来创建新视图。签名将是 +(UIView *)viewWithType:(ViewTypeStyle)viewTypeSyle)

Also note, that you are using the Factory pattern. You should use a class method to create new views. The signature would be something like +(UIView *)viewWithType:(ViewTypeStyle) viewTypeSyle)

.h

#import <Foundation/Foundation.h>

@interface TableController : NSObject <UITableViewDataSource,UITableViewDelegate>

@end

TableController.m / p>

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

这篇关于在运行时实现代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆