First iPhone App – Hello World
Introduction
In previous tutorial First step to create an iPhone app, we learned about the basic document structure about iPhone app and roles and importance of diff diff files.
What will you discover from this tutorial is as following :
- Overview of the syntax and structure of objective-C.
- You will get to know how you can conceive UI (User Interface) with the built-in interface builder.
- How to build, amass (compile) and run the application.
This is how your application will look like when you complete this tutorial is as below :
If you have read our preceding tutorial then you had effectively created “HelloWorld” app. if you did not read that proceed through it. here is the link : First step to create an iPhone app
Now Open your Project using Xcode.
Now we are going to run our app for first time but before we run the app, check below image
- First button stands for “Build and run current scheme”
- Second button is used to stop the running scheme or application
- In third section you set the Active scheme (Here we run our app in iPhone 6.1 simulator)
- it stands for activating breakpoints
So you can run your application by clicking on First button (As per above image). now you will see blank screen with gray background.
Up-till if you have follow preceding tutorial and overhead points now you should have a better concept about interface and implementation. so now Let’s go back to the code.
Go to ViewController.h file.
In Objective-C interface of class are organize in .h file.
@interface We are using this keyword to declare the interface of a class.
Lets modify the .h file as per below.
@interface ViewController : UIViewController { NSString *Name; // Member Variable declaration } // This is method declaration where. -(IBAction)Show_Message:(id)sender; @end
Check out below image for more clarification of method declaration.
#import "ViewController.h" @interface ViewController () @end @implementation ViewController // This method viewDidLoad is called only once when view controller is loaded in to memory. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(IBAction)Show_Message:(id)sender { // UIAlertView Class is normally used to show the alert to user. To create an UIAlertView we are using following lines of code. // Create an object of an UIAlertView named "Alert" and alloc it using // "– initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:" method. its pretty clear below lines UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Hello world" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; // using this line we showing alert. [Alert show]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
With this we have done with implementation part.
Now only thing remain is now “create an button on which button click event we have to show the alert.”
Now, move to the “ViewController_iPhone.xib”.
This gray background kind of thing is your “UIView”.
Lets Drag and Drop a button in to UIView.
To rename its title to “HelloWorld” [To rename it simply double click on the button, and start typing your text.]
Now we are going to establish an connection between “HelloWorld” button and “Show_Message” method.
Go to ViewController_iPhone.xib–>File’sOwner –> Connections inspector as shown in below image.
Here you will a section named “Received Actions“, under that our method name “Show_Message“.
Now see the below image and connect it with the “HelloWorld” button.
Drag a method connection towards to “HelloWord” button.
Select “Touch Up Inside”
This is how your connection inspector look after “Show_Message” method connection to “HelloWorld”.
Its DONE !!!
Run you program. Click on “HelloWorld” button. it will show you the alert as it display in below image.
In the event you have any query, Feel free to leave comment ! 🙂 To download the source code click here. In Next POST, We will start to Build an app with Table View.
Have fun !!!