Creating the first project for iPhone.

Saturday, January 21, 2017 Saurabh Shrivastava 0 Comments

Be it anything in the world, First thing always excites you.As this is my first blog, I am to introduce you to the very first project for iOS and glimpse of swift 3.0 language. Later on I'll introduce the basic concepts in the iPhone programming. So here it is in front of you :)

Before I start writing, I'll introduce you to the IDE, I am using Xcode 8.2.1 for development. There are  various terminologies associated with it, across which I will come during the development of the posts.


You have to start with a new project, as described in the above image.


The next part is to select a template from the various templates, for now I'll take single view project.

above image  describes the various informations,w which are entered while creating a new project.
  • Product Name: is the project name that is given for the project.
  • Team : Team name (Using which membership is taken)
  • Organisation identifier: Reverse domain identifier that uniquely identifies the product.
  • Language: IN which you want to write the application(Objective-C or Swift). Here will opt out swift as our current language.
Now will click next to create the project.


Since the very first project's created, there are few points worth knowing. App delegate class is the base class, where execution starts. After main method is invoked,it transfers control  to App delegate class and the very first method to be called is willFinishLaunchingand subsequently didFinishlaunching method is called.  

willFinishLaunching method is  used for pre launch initialisation if needed. After this window for the application is set up.

Initial view controller, defined in Main.storyboard is loaded.  Main.storyboard is the file which has the UI screens defined as a scene. Every scene has a master class for it. A story board  file looks like in the below image.  In the editor area A scene is shown, it is of type ViewController and an arrow in the left side of the scene indicates it's the initial screen to be launched if application runs.



Now on view what will do is, I will add one Text field, in which you can enter what ever you want, a label to show What did you enter on press of a button.
So here we go :-)

Now in ViewController.swift file inside class declaration  declare outlets though which controllers will control UI components inside  a view.

@IBOutlet weak var txtEnterText:UItextField!
@IBOutlet weak var lblEnteredtext:UILabel!
@IBOutlet weak var btnShow:UIButton!

I've created three outlets for each of my UI component placed on my view. now go to last tab of inspector(in the right side of XCode). There you will see the outlets which you have created n the controller file. that may wonder you how have they appeared there, as I'd mentioned for every scene There must be a controller. and how one controller should be linked to a Scene is described in below image.


In the class section of a Scene, you can see ViewController. Go to the last tab of the inspector
 and




Since all the connections are done, let's go to view controller file and write some code to handle the press of the button.

    @IBAction func buttonShowAction(button:UIButton){
        if (self.txtEnterText.text?.isEmpty)!{
            self.lblShowtext.text = "You haven't entered anything."
        }else{
            self.lblShowtext.text = self.txtEnterText.text
        }
   }

In the ViewController.swift file write the method button show action and go to IB inspector. @IBAction (Interface builder action will show the method here in inspector). Drag from it to button and a popup will show to choose the action, select touch up inside. 
fun is the swift keyword to write the method.

Now press command+R to run the application.



Bingo! We have done it, In next blog will go through the basics of he swift language and how can we customise the experience.

Thank you :)



















0 comments: