Re: Regarding MVC design pattern
Re: Regarding MVC design pattern
- Subject: Re: Regarding MVC design pattern
- From: Quincey Morris <email@hidden>
- Date: Sun, 16 May 2010 22:33:21 -0700
On May 15, 2010, at 10:56, Sai wrote:
> Suppose I have an iPhone application follow
> the MVC design pattern.
> The Model is presented by an custom object. And I have declared an instance
> of the Model Object as a IBOutlet
> in my Controller class. I found that every time I start my application, this
> instance of Model Object will be initialized.
> My first question is who called this init method, what for?
You seem to be saying you "instantiated" your data model by adding a custom object (blue cube) to your XIB file in IB, and changing its class to Model, with your Controller class as File's Owner.
When you do that, IB creates an instance of Model for you, whenever that XIB file is loaded. We don't know *when* it's being loaded, because you didn't tell us which XIB file, or give any information about Controller. (An application can have lots of controllers. Some may exist for the life of the application. Some may exist only while a window or view exists. Some may have other lifetimes.)
So the answer to your first question:
Your Model is being initialized because the NIB file that contains it is being loaded. The gory details of what happens can be found here:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html
> As I found that it will be initialized automatically, so I just send message
> for that instance, and crashed at runtime
> with a exc_bad_access signal error. So I have to call alloc and init for
> that Model Object and assign the returned
> instance to the IBOutlet variable, then everything runs well.
> My second question is if I have to allocate and initialized that Model
> Object for myself? Is it over-done for this
> initialization because it is called automatically before.
We don't have enough information to answer this question. Almost certainly, this is a memory management error. The Model object is getting deallocated too early.
Or, you're trying to reference your Model object too early: since it's created when the NIB is loaded, it doesn't exist until after the NIB is loaded.
> My third question is where I should call the release method for this
> instance of Model Object?
Probably never.
If the Model exists for the life of your application, then you never need to release it.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden