The inevitable HelloWorld application is a staple in learning a programming language, and provides the learner with the feeling of accomplishment as their first program speaks back to them… or something. Either way, this example will present us with a basic framework which we can use to build upon.
To break it down this example consists of,
– Creating a blank project in Xcode
– Using the default Delegate class and adding our own method (interface)
– Linking the GUI to our class
– Adding code to our method (implementation)
– Drinking tea
Creating a project in Xcode
This is pretty straight forward, open Xcode and Select Cocoa Application as the template to your new project. I’d recommend having a folder to contain all of your development projects in, however file management is your own business. To keep within the naming used by this example I would recommend calling your project HelloWorld though.
Once the project and it’s associated files have been created you’ll be presented with Xcodes main interface, which will have references to numerous frameworks and contain the files that we are really interested in (MainMenu.xib, HelloWorldAppDelegate .h/.m)
Using the default delegate class
It is possible to either create your own class or use the one that is now created by Xcode, given there is one created it makes it easier just to use it. In the main Xcode if we open the tree under HelloWorld and open the ‘Classes’ folder we’ll see two files that are the HelloWorldAppDelegate files. If we open the header we’ll be presented with the interface, which contains the declaration of the class HelloWorldAppDelegate. We can see that this object inherits from the NSObject class and comes with one variable declared, along with the @property declaration (can be ignored for now).
We will modify this class declaration by adding a method to it, this method will be called ButtonPressed (it will be called, when a button is pressed….) and should be written as followed:
This needs to be written after your variables have been declared but before your @end declaration, otherwise it will be out of scope for your class. The next step is to write the code that will be run (implemented?) when this method is called. As mentioned previously code that is to be run needs to be part of the @implementation declaration, which is in the other file that makes up your class. In this file you should find a method that is inherited from the parent class NSObject and we can ignore this method as we’ve no code to add to this being called. Under this method we’ll be adding our own method, which for the time being will look as followed:
// Code to be run
I would recommend compiling your modified source code, just to ensure that you’ve not made any syntax errors and also as it will save your changes that we’ve made so far. Should everything compile fine, it’s time to add a button on our blank GUI and link it to the class allowing it to call our method ButtonPressed.
Linking the GUI to our class
To link our code and our interface we’ll need to open the MainMenu.xib in interface builder and set up the various links between the GUI and the code. In Xcode clicking on the HelloWorld project tree object (above the Classes tree object) will again present us with all of the files that are part of our project and in here you’ll fine the .xib file. Double clicking on this file will start up the Interface Builder application and present us with the default window and menu bar that make up the GUI, along with the Interface project window containing the elements that make up the interface.
You should see that a class has been added to the interface builders window (presented as a blue cube), the class Hello Wold App Delegate is added automatically by the Xcode project creator(in future posts we’ll be adding our own classes). From the library window find the Push Button Object and drag that onto the blank window and then control+click on the button and drag down to the Hello World App Delegate Class.
This will present you with a little drop down showing all methods available to the button and here you will see ButtonPressed, which you should select. This button is now linked to the Method ButtonPressed and we can now save our changes and quit from the Interface builder.
Adding code to our method (implementation)
At this point everything is linked up and declared, so compiling this code will actually call the method in our code when the button is pressed however at present there is no code in the method and therefore nothing will actually happen. So for our example we will use the NSLog function which allows us to print out messages to the system log (usually for debug purposes) however we will be printing out “Hello World”. The implementation of the ButtonPressed method will need the following adding:
Once this code is added open the Debugger console in Xcode by going to the Run menu item and finding Console (also by pressing Shift+Apple Key+R), and then selecting the ‘Build and Run’ option.
Enjoy a nice cup of tea whilst repeatedly pressing the Button and watching “Hello Wold” appear in the debug log.