Monday 26 November 2012

Well, to start with we assume that you have basic understanding of C and C++. Trust me don’t jump to iPhone tutorials without knowing basics of C/C++, you will be lost. Another requisite is to own a MAC. You can’t code an iOS program on any other platform other than MAC. If you have these things ready then let’s get started with our tutorials.

Before starting let’s get our playground ready. Go ahead and download and install the iPhone SDK from the Apple Developers section. You should sign up as an apple developer in order to download it. This SDK will have everything you need to build the app. It contains:
1) Xcode – This is the IDE used to develop the apps.
2) iPhone Simulator – A simulator to run you apps.
Downloading the SDK is free but if you want to share your apps on the apple app store you will have to pay them some amount.

Now since you have your playground ready lets fire it up. Go ahead and start your XCode. The first screen you will see is the welcome screen. Here you can select the various options such as creating a new project, connecting a repository etc. Since this is your first time you won’t have any recent projects listed. For now click on create a “new project”. You will be presented with intimidating screen as show below. Don’t be alarmed, you can find more detail of all these on apple documentation as for now click “Window-Based Application”.
This will give us a Window and an application delegate. Delegation is the simple yet powerful concept of handing a task over to another part of the program. In object-oriented programming it is used to describe the situation where one object defers a task to another object, known as the delegate. The ios coding used UIApplicationDelegate. It is an object that communicates with UIApplication object. Select “choose” and you will be prompted to save the code. Just give it a name and save it wherever you want. I have named mine KaavayWorld and saving it in Documents.

Once the project is created, you'll be presented with the XCode interface and all of the files the project template has generated for you.
I know it generates a lot of files but you don’t have to use all of them to create this app. The only files you should be interested in are the main.m, KaavayWorldAppDelegate.h and KaavayWorldAppDelegate.m. The main function is like any other main in C or C++. Here it creates a UIApplication object. The function call UIApplicationMain() does the work of creating the object. If you are wondering how KaavayWorldAppDelegate and UIApplication are linked, then let me tell you the secret. There is file MainWindow.xib, you call it as MainWindow nib file (yes, it’s not a typo error. It is written xib but called as nib file). This file does all the linking between the two.
That’s all for the main, now let’s jump to our delegate files. In iOS your code is always broken down into two files “.h” called as the header files and “.m” files called as the implementation files. Click on KaavayWorldAppDelegate.m. There is nothing interesting is KaavayWorldAppDelegate.h file. Xcode has already written most of the code for you. Just jump to the function called applicationDidFinishLaunching. In iPhone programming the function names are long like hell. This is just the start.



This is the function that gets launched first when the application starts. Currently it is just making the window visible.
For our Kaavay World application we need to create a View Controller. Right click on Classes folder and choose “Add->New File”. This will give you the New File Window. Click on “UIViewController” and say “next”
This function is where we'll be creating our view controller, which will eventually hold our 'Kaavay World' label object. In order to create a view controller, we need to add another class to our project that subclasses UIViewController. Fortunately, since creating view controllers is a common task, XCode has a template for it. Right mouse click on the Classes folder and choose Add > New File.

When you click Next, you'll be presented with some options. The only thing you should have to set is the filename. I named mine KaavayWorldViewController. You might notice that the new implementation file (KaavayWorldViewController.m) is full of commented out functions. View controllers are meant to be used by overriding the base implementation of various methods. In our case, we want to override loadView, which is used to manually populate a view controller.



In loadView() method create an object of CGrect and call it myView. This will be the boundaries for our view. You can give any values you want as long as it doesn’t go beyond the view area of an iPhone. I have given 0,0 to 320,480 which is the max., the code for all the above is given below. Now what we will do is add this created view to the existing view which was created for us. We do this by saying



Now we create our label which will display the text Kaavay World. So we create another CGRect object for the label. Create a UILabel called label. Allocate it some memory and initialize it with myView. Next we assign the Kaavay World text. We do this by saying



Here we are using the UIView, allocating it some space and initializing with myView.



After that we add the label as subview to the current view. And then release the label. It is good practice to release the variables you’re not using otherwise it creates memory leaks. There is a concept called as Automatic Reference Counting (ARC) is that is enabled the iOS takes care or releasing the variables.
We now have a view controller with a label that will display the text, "Kaavay World". Now we need to create an instance of this view controller and add it to our application. Remember that function, applicationDidFinishLaunchingthat I mentioned earlier?? That's where we'll be creating our view controller.
The first thing you're going to have to do is add an import statement at the top of that file (KaavayWorldAppDelegate.m) so the compiler can find the declaration of that object.



Just add it right under the existing import statement. Now we're ready to create the view controller.



Just like before, we simply allocate a new KaavayWorldViewController. We don't add the view controller directly to the window; rather we add its view property to the window. Technically we could create the view controller locally, but doing so would cause a memory leak. We need to save a reference to it somewhere so we can clean up the memory at a later time. I created a property called viewController to store the view controller.
Properties are defined in the .h file. Here's my modified KaavayWorldAppDelegate.h file.



Just add it right under the existing import statement. Now we're ready to create the view controller.



The @class KaavayWorldViewController is a forward declaration. Basically we're telling the compiler a class with this name exists, but you don't need to worry about its definition right now. In the @interface section, we declare a member variable to hold our view controller. Lastly, we use @property to wrap our member variable with implicit get and set functions.
We're almost done. We now need to tell the compiler to actually create the get and set functions for our new property. We do that back in the .m file with @synthesize.



Just add it right under the existing import statement. Now we're ready to create the view controller.



You should already see one for the window property. Just stick this one right underneath it. The very last thing we need to do is release our reference to the view controller when the dealloc is called. There should already be a dealloc function in the .m file. We just need to add a little to it.



And there you have it. The final KaavayWorldAppDelegate implementation file should look like this:



We're done. If you build and run the app (Build > Build and Go), you should see something that looks like the image below.
Let us know when you are done learning this. We have much more complex stuff to teach you. All the best!