iOS

Want To Learn iOS App Development, Here Is A Comprehensive Guide To Create A Simple App

Apple has made great strides in the IT domain by launching the latest and highly intuitive programming language Swift back in June this year. With this release, the application development has become quite convenient and more efficient.
img1
Indubitably, Swift is much modern, robust, and secure programming language that has made programming a fun and it can be expected as the future of the development realm. Hence, learning programming in Swift could be quite beneficial.

For the folks, who are beginners in the field and interested in learning this futuristic development language, this article will provide a comprehensive guide to create a simple iOS application using Swift to display “Hello World”.

Before beginning, ensure that you are equipped with the latest Xcode 6 version and it is assumed that you have some basic programming knowledge.

Without any further ado, let’s start app development.

1. How To Create A New Project:

img2
• In the Xcode IDE, navigate to the File tab and under the New tab select Project.
• This will open a small window showcasing some templates, click on the Single View Application option. Then, simply click on the Next button.
• After doing so, you will need to fill some details in the appropriate fields like Product Name, Organization Name, Language, Devices, and more. Fill the details and click on the Next button.
• Just save the project to a desired location and move to the next step.

2. How To Design An Interface:

img3
By default, the Xcode creates certain files for us, these files can be accessed through the Navigator pane, available on the left side of the screen.

• Open the Main.storyboard file, it will display a square “View Controller” which can be assumed as a screen.
• To change its size and make it equivalent to an iPhone, you can make the appropriate tweaks under the Attribute Inspector of the View Controller.
• To introduce a button in the application, simply access the “Object Library” and drag and drop the Button option from the available list of options (Library) into the View Controller.
• To set a desirable title for the button, simply double click the button and name it as “Tap me”.
• To view the so created interface, you can click on the play icon available in the Xcode and watch how it will appear on a simulated device.

3. How To Write The Swift Code:

img4
• Navigate to the “Assistant Editor”. This will open the ViewController.swift file on the screen possessing a few lines of code. In the available code, consider the viewDidLoad() function that will be called after our defined view will get loaded. Since, we are working on a simple application that is featuring only a single view, the function will be called as soon as the application is initiated.
• At the end of the function append the following line of code to view some change on the screen during app execution.

println("Hello world")

This code will print the text “Hello World“ on the screen.

4. How To Integrate An Action:

Here in this step, our objective is to ensure that the message created in step 3 gets printed on the screen only after a user taps on the earlier created “Tap Me” button. For this just follow the below mentioned steps.

• Simply hold the Ctrl button on the keyboard, click on the “Tap Me” button and drag the button above the viewDidLoad() function in the ViewController.swift file.
• After doing so, you will get a big list of options in front of you. Here, you are required to tweak just two of them –

◦ Connection: Action
◦ Name: buttonTapped
• Click on the “Connect” button. This will add the following code in the swift file.

@IBAction func buttonTapped(sender: AnyObject) {
}

Since, the above created action is attached to the button, the code available in this function will get executed whenever the button is tapped.

Let’s insert a println function inside the buttonTapped() function:

@IBAction func buttonTapped(sender: AnyObject) {
println("The button has been tapped")
}

Hence, if now you will run the application, for every tap on the button the simulator will display the message “The button has been tapped” in the console.

5. How To Create An Outlet:

Outlets basically facilitate developers to change the interface elements via a suitable code.

To check its implementation –
• Just drag the “Label” object from the Library to the Interface (View Controller), and hold the Ctrl key, click on the label object and drag it to the ViewController.swift file.
• Set Controller as “Outlet” and Name as “myLabel”. This will add the following line of code in your code.

@IBOutlet var myLabel: UILabel!

• Now tweak the buttonTapped() function and replace the println() function with the following

myLabel.text = "The button has been tapped"

Now, when you will run the application in the simulator. After tapping on the “Tap Me” button, you can observe the value of label changing to a static value displaying the string ”The button has been tapped”.

Follow these simple steps and create a simple iOS application to display “Hello World” and learn how label and other action can be created.