
Giovambattista Fazioli
CTO
Saidmade Srl
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?

Jonathan Lehr
President
AboutObjects
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

Giovambattista Fazioli
CTO
Saidmade Srl
Ok,
them, What is the real different between Delegate and Controller?
Thx

Jonathan Lehr
President
AboutObjects
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

Giovambattista Fazioli
CTO
Saidmade Srl
WoW, now is clear!
Thx