In this tutorial I’m going to teach you how to write your first iOS app(aka Hello World). I will assume you already know logic programming. If you’re reading this, then you’re probably not familiar with Objective-C and maybe even scared of it! Objective-C looks scary at first, but once you learn it, you won’t want to write code in any other language. It’s just easy, organised and readable. But why people find it so scary? Because of its unusual syntax! In Java, you’d write something like this:
In Objective-C, that would be:
It looks nothing like C, Java, PHP or other popular languages. But all you have to do is to open your mind and start from simple things, like this app I’m going to teach you how to write.
Start by opening Xcode and clicking on File > New Project. Select “Single View Application” and click on Next.
Give your project a name(in my case, I named it “Hello World”), enter an organisation name(e.g. your own name), select the device you want your app to run on(iPhone, iPad or universal) and click on Next. Pick a location to save the project.
Now open the main.storyboard. There’s where our app interface is. What I really like about iOS is that Interface Builder is great, easy and things just work(unlike on Android). You can also build your interface programatically, but that’s not recommended since it takes more time and is more likely to lead to errors. From the box you on the bottom left, you can drag and drop interface elements to the app view. Drag a button(UIButton) and a label(UILabel). Resize and place them on the right place.
Now let’s get to the code. Open FVViewController.h.
Add a pair of curly braces to the end of this line(in case they’re not there already):
@interface FVViewController : UIViewController
You can declare your variables and pointers between the curly braces. Under the curly braces you can declare functions and methods. Note that in Objective-C #import is far more used than #include.
Now let’s declare our label. Add this line between the curly braces:
IBOutlet UILabel *label;
When you add IBOutlet before the pointer type(in this case, UILabel), Interface Builder understands you want it to see your pointer. Since we’re using a UILabel, we used the pointer type UILabel. If we were using, let’s say, a UITextField, we’d have written that instead. And finally, label is the name of our label. I could also have written something like textLabel or textContainer.
Write this line bellow the curly braces:
IBAction is the same as void. When you want Interface Builder to see a method, you have to use IBAction as its return type. Just like IBOutlet. The sender will be the interface element that invoked that method. id is a pointer type that can be anything. It’s used when we don’t know what we’re pointing to. An id could point to a NSString, NSArray, UILabel, UIWebView and so on. Despite being a pointer type, there’s no need to put an asterisk.
Now go back to Interface Builder. Control click on the view controller and you will see a list. Drag and drop the little circle on the “label” line to the label on the interface. Then under “received actions”, you will find our method “click”. Drag and drop its circle to the button. You will see a list of event types. In our case, we want the “Touch up inside” one.
[label setText:@”Hello World!”];
The first line is the method name. Remember what I said about syntax in the beginning of the text? The second line explains it self. label is the object, setText: is the method and @”Hello World” is the argument. In this case, the argument is a NSString. If it was a mere C string, we’d have written “Hello World”(without the @).
Objective-C is not as scary as it seems. If you were able to write this app and understand what you wrote, you will be writing apps in no time. You can download the project by clicking here.