tags:

views:

100

answers:

3

Another noob question, but while looking at some code examples, I see stuff like

UIViewController *controller = [[UIViewController alloc] init...

Making an instance of the class UIViewController which I understand. But then you have these instance variables of your own class. Like let's say I was building a simple counter program called Counter and it counts when you touch the screen and I have a property for my view controller called "count." So when I run my program, is that when my instance of my class is created? Because it's not like I ever create a Counter *counter object. Thanks.

+1  A: 

So when I run my program, is that when my instance of my class is created?

Yes, when your program executes the objects are created.

Mr-sk
A: 

If you have allocated and initiated counter but are using it, I'm surprised anything works.

You allocate and initialize objects before you use them and then release them when you are done.

Jay
A: 

I think you may be confused because of the way loading nib (XIB) files instantiates objects without any actual Objective-C code to instantiate the object.

Read about the NIB Object Lifecycle in this guide: http://beta.devworld.apple.com/iphone/library/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW18

Basically, the first NIB gets loaded based on whatever is specified in the YourAppName-Info.plist for "Main nib file base name" and that will instantiate any objects defined in there and then set IBOutlets to point to them as specified in the NIB file. As a result, certain objects will seem to just "be there" referenced in the outlets (like IBOutlet UIWindow *window in your app delegate) by the time your code executes.

I recommend the book Beginning iPhone 3 Development as a great tutorial for all of these things.

Nimrod