Sunday, 28 December 2008

For starters...

Anybody who starts with programming makes always the same first little program : the 'Hello World' application.

Well lets see how this works in Objective-C.

First start Xcode.
It's always a good practice to put your projects and builds in seperate directories. You can change this in XCode>Preferences>Building. 
Change the Build Products directory and the Intermediate Build directory.

If you done that , you can create a new Project (via XCode > File > New Project ) the project assistant will popup, choose 'Cocoa application'  and  call your new project HelloWorldProject.


As soon as you did that a project browser will show up (see below). 













Surprise ! You see a lot of files here, all this is generated by XCode.

Don't worry about them yet.

Let's first build our little HelloWorld application.

Start by double clicking the MainMenu.nib file, this will open the Interface Builder. When the interface builder, again a number of windows will open :
1. A blank window in which you'll design the UI
2. A palette with all types of UI controls (Cocoa -controls)
3. A menu-bar
4. The mainmenu nib itselfs

Start with dragging a text field from the Cocoa-controls to the blank menu (the text field controls can be found by clicking the third item on the menu bar of the Cocoa-controls window).

Now the first part of the helloworld application is done.

The second part consists of the creation of a controller. Now here we need some theory.
In fact what we want is an implementation of the Model-View-Controller design pattern.

In our case we have built a View with the Interface Builder and now we need to put some logic behind to do something useful. This logic we will put in a controller.

The setup of a controller is again pretty straightforward :

In the Mainmenu window you select 'classes' and look for the class NSObject (see screenshot below) 


Now up in the menu you select 'Classes>Subclass NSObject'. A new class will appear , you can rename it to HelloWorldController.
Now we need to create the files in which we will write Objective-C code.
Therefore you you go to the menu and select 'Classes>Create Files for HelloWorldController'. A popup will appear, let everything default and press the 'choose' button.

The next thing you must do is to instantiate that class (otherwise your newly created view won't work).

To do this you first select your newly created controller (HelloWorldController) and then you select from the menu 'Classes>Instantiate HelloWorldController'.

Voila we are almost there ! Now we need some coding.

A first step is to create an outlet in the controller class. This you do by selecting the HelloWorldController class in the class browser.
And then in the menu you select 'Classes>Add outlet to HelloWorldController'.

A popup will appear in which you can define the outlet (call it sayHello) .

Once the outlet is created we need to link it to the text field.

This is done by selecting the HelloWorldController instance in mainmenu window.
You then CTRL-Click the HelloWorldController (keep mouse button pressed) and drag to the text field (a blue line will appear). Release the mouse and a popup will appear, in this popup you see all the outlets defined (in your case only one) .

Now connect the sayHello outlet by clicking the 'connect' button.

Now we are done with interface builder, use Command-Q to quit it ( he will ask first to save all your work).

In the XCode project browser you'll see 2 new files, HelloWorldController.h and HelloWorldController.m.

In the .h file you should add the following line (between the { } ) :

IBOutlet id sayHello ;

In the .m file you should add the following method  (after the @implementation line) :

- (void) awakeFromNib
{
[sayHello setStringValue: @"Hello World" ];

}


Now build and run this application, and YESSSS you see that 'Hello World' appears




 


 


No comments:

Post a Comment