Sunday, 5 May 2013

iOS Programming Basic: How Does the Hello World App Work?

Before  start you may come across these questions:
  • What are those .xib, .h and .m file?
  • What are those “ugly” code inside “showMessage”? What do they mean?
  • What actually happens after you taps the “Hello World” button? How does the button trigger the “showMessage” action?
  • How does the “Run” button in Xcode work?

 

Interface Builder, Header and Implementation Files

.xib – For files with .xib extension, they’re Interface Builder files that store the application’s user interface (UI). As you click on the .xib file, Xcode automatically switches to the Interface Builder for you to edit the UI of the app via drag-and-drop.

.h and .m – Files with .h extension refers to the header files while those with .m extension are the implementation files. Like most of the programming languages, the source code of Objective-C is divided into two parts: interface and implementation.  

In Objective-C, interfaces of a class are organized in “.h” file. We use the syntax “@interface” to declare the interface of a class. Take a look at the HelloWorldViewController.h, which is the header file:

@interface HelloWorldViewController : UIViewController

-(IBAction)showMessage;

@end
The actual implementation is put in the HelloWorldViewController.m, the implementation file:
@implementation HelloWorldViewController

// I've removed other methods for better reading. Focus on the showMessage method first.

- (IBAction)showMessage
{
    UIAlertView *helloWorldAlert = [[UIAlertView alloc]
                                    initWithTitle:@"My First App" message:@"Hello, World!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
   
    // Display the Hello World Message
    [helloWorldAlert show];
}

@end

 

No comments:

Post a Comment