开发者

Linking Model to Controller in simple application?

I am just looking at ways to access a simple model object (in the MVC sense) from my controller. Right now I am creating the model in the applicationDelegate, and passing it to the controller when I create the controller.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Setup Model
    DataModel *tempDataModel = [[DataModel alloc] init];
    [self setDataModel:tempDataModel];
    [tempDataModel release];

    // Setup Controllers
    Controller *rootController = [[Controller alloc] initWithModel:[self dataModel]];
    UINavigationController *tempNavController = [[UINavigationController alloc] initWithRootViewController:rootController];
    [self setNavController:tempNavController];
    [rootController release];
    [tempNavController release];

    [window addSubview:[[self navController] view]];
    [window makeKeyAndVisible];
    return YES;
}

inside the controller I have:

@property (nonatomic, retain)DataModel *dataModel;

and:

- (id)initWithModel:(id)newModel {
    self = [super init];
    if(self) {
        NSLog(@"%s", __PRETTY_FUNCTION__);
        dataModel = [newModel retain];
    }
    return self;
}

- (void)dealloc {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [dataModel release];
    [super dealloc];
}

This works fine, but I am just cu开发者_Python百科rious if this is ok in terms of MVC and good design. In previous apps I have:

  1. Used a shared instance (Singleton)
  2. Created the model from inside the controller.

Any comments would me much appreciated:


I think this is perfectly good design. The controller is allowed to manipulate the model, so needs a reference to this. I think your current way of injecting the Model instance is better than a singleton approach.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜