Community > Forums > General - Announcements > iPhone Default Sketch Flow
July 10, 2009 3:35:06 AM PDT (one year ago). Seen 420 times.
Photo Giovambattista Fazioli
CTO
Saidmade Srl
Member since Jul 10, 2009
Posts: 11
Hi,
When I build a very simple iPhone app, with a delegate and one view controller (without use Interface builder), ir't correct to insert all main code in delegate file?
July 10, 2009 5:39:19 AM PDT (one year ago)
Photo Jonathan Lehr
President
AboutObjects
Member since Mar 12, 2009
Location: South Riding
Posts: 22
It's okay to start that way if you're just experimenting, though it really depends on what you want your app to do.

However, putting all your code in the app delegate is probably not the best approach for writing a real app. For example, if you're creating a view programatically, it's best to do that in your view controller's loadView method.

The loadView method is called automatically by the framework when your view needs to be loaded, so this approach doesn't require you to write any additional code.

- Jonathan
July 10, 2009 6:46:29 AM PDT (one year ago)
Photo Giovambattista Fazioli
CTO
Saidmade Srl
Member since Jul 10, 2009
Posts: 11
Ok,
them, What is the real different between Delegate and Controller?

Thx
July 10, 2009 7:35:45 AM PDT (one year ago)
Photo Jonathan Lehr
President
AboutObjects
Member since Mar 12, 2009
Location: South Riding
Posts: 22
First let me amend slightly what I wrote previously. The loadView method is called automatically if your view controller is being used with a navigation controller, tab bar controller, etc. Otherwise, you can send your view controller a viewWillAppear: message as needed.

The default implementation of viewWillAppear: in UIViewController checks to see if the view controller's view has already been loaded. If not, it sends itself a loadView message. You should never call loadView directly, because that would omit this check.

The application delegate is not an instance of a specific class; it is an object that receives notification messages such as applicationDidFinishLaunching: and applicationWillTerminate: from the application object (which is an instance of the UIApplication class). View controllers on the other hand are instances of UIViewController and its subclasses.

So it's possible for the application delegate to be a view controller, though that usually doesn't make much sense from a design perspective. Think of the app delegate as a sort of global controller for your application. It could for example save your app's state when the app quits, and restore it again when the app starts up.

A view controller manages an instance of UIView, and is responsible for managing the specific portion of your app's user interface represented by that view and its subviews. An app typically contains a number of view controllers.

A view controller is responsible for loading and initializing its view, preparing it to appear onscreen and to disappear from the screen, handling changes in screen orientation, for example by laying out its subviews, and responding to user touches. The best way to get insight into the roles of the app delegate, view controllers, and other components of Cocoa touch is to read Apple's documentation. In particular, you might want to take a look at their View Controller Programming Guide.

- Jonathan
July 10, 2009 7:47:23 AM PDT (one year ago)
Photo Giovambattista Fazioli
CTO
Saidmade Srl
Member since Jul 10, 2009
Posts: 11
WoW, now is clear!
Thx