Friday, January 6, 2017

iOS Development Tutorial - Cat Years App

In this tutorial, we're gonna learn how to create a Cat Years app. We're going to find the Cat's age in this app. Our app allows you to enter any number and it's multiplied by seven. Then it is shown above the cat image. Let's get started!




Steps:

  1.  Open the Xcode from the launchpad.
  2.  Go to file -> New -> Project.
  3.  Then you've to click Single View Application template.
  4.  Enter your product name(app name), organization name, identifier.
  5.  Select Swift as your language.
  6.  Finally, set iPhone as your device.

Now in left pane, just open the project navigator and click the Main.Storyboard file. It'll open the device in the interface builder.

It's time to drag and drop your views(Label, TextField, Button, ImageView) from Object Library(it's in right pane) into View Controller(Layout). See the image below and do it like that. I hope it's easy for you...




Then click the TextField view and add "enter any number" as placeholder from the Attribute Inspector(It's in right pane) & also set keyboard type as Number Pad.

Now open the ViewController.swift file. Then start writing your code like below.
 

    import UIKit

    class ViewController: UIViewController {

    @IBOutlet var textField: UITextField!
    @IBOutlet var ageResults: UILabel!
    
    
    @IBAction func submitButton(_ sender: Any) {
        

       // print(textField.text!)
        
      var myVal = Int(textField.text!)! * 7
        
        ageResults.text = String(myVal)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

By default, ViewController.swift file comes with viewDidLoad and didReceiveMemoryWarning methods.

viewDidLoad() - This method is called, when the app starts and shows the layout to you. You can check this out by way of printing( write this print("viewDidLoad method called") method inside the viewDidLoad method ) something in the console.

didReceiveMemoryWarning() - This method is called when the device receives memory warning, it means the app force stopped due to low memory on your device.

We're just creating a variable called myVal. Then store the integer value which is taken from TextField & multiplied by seven and type cast it into Integer.

Simply assign the myVal values to ageResults variable by type cast the myVal into String data type. That's all.

Now, To run the application click the Run button(it's in left top corner or just click the CMD + R to run your project).  Okay our app is ready to start and I know you're eager to find our cat's age :)

Output:





Download the project from GitHub

Happy Coding! See you in the next tutorial..

Location:India, Tamil Nadu, Tuticorin Thoothukudi, Tamil Nadu, India