Showing posts with label Apple. Show all posts

iOS Development Beginner Tutorial 3 - Working With Storyboard

In this iOS Development Tutorial let's go deep into the Storyboard concept that Apple introduced few years ago.
Let's create a new project & here you can see the default Storyboard screen.




You can change the default Storyboard to your custom created Storyboard in Project Settings --> General Tab --> Main Interface



If you are going to create an App with only Single screen you can use this default screen as your single screen. But most of the times you will end up with so many screens in the app. To Navigate through those screens iOS Apps have 2 default ways. They are,


  • Navigation Controller
  • Tab Bar Controller
Here is an example of Navigation Controller in iOS.
 
 
 Here is an example of Tab Bar Controller in iOS.


Now you have an idea of what are those Controllers. Let's implement Navigation Controller first in our sample App.
Select the ViewContoller View you want to add Navigation Controller & then go to Editor in the Menu bar & select Embed In.  Now you can see both Navigation Controller & Tab Bar Controller. Select Navigation Controller from there.
 
 

Now you can see Xcode automatically creates the Navigation Controller for you.
To Show you how Navigation Controller works, I'm going to add a new ViewController to the StoryBoard. You can easily add a ViewController by drag & drop a new ViewController from here.
 

 


And then I'm going to change both ViewController Background colours.
 
 


Then I'm going to add a Button to the 1st ViewController & when I touch that Button I want to navigate to the 2nd ViewController.
 
 
To setup the navigation Press Control on the keyboard & drag the Button to 2nd ViewController. Now you can see a dialog box as follow.
  
In that dialog box select Show. Now you have completed the Navigation controller combination with Storyboard. Here is how it works in the Simulator. 
 
It automatically creates thousands of codes & you just need to put a button to create the navigation. 
Tab Bar also works as same. Let's add a Tab Bar Controller to 2nd ViewContoller. After adding it, the storyboard will look like this.
 
 


Let's add 2 more ViewControllers to the storyboard & Now I'm going to add those 2 new ViewControllers to the TabBar Controller. 
 
 
To add new ViewControllers to the TabBar Controller press Control button on the keyboard & drag TabBarController to a new ViewController.
  
In that dialog box select View Controllers. Do the same steps to the next ViewController as well. Now you can see it automatically creates all the connections related to Tab Bar Controller.
 
 
To see this, let's run the project in a simulator. Here you can see, when you navigate to the 2nd ViewController there is a Tab Bar inside the View & you can switch between Views using the Tab Bar.
  
This is how the Navigation Controller & Tab Bar Controller works in Storyboard. If you are not using Storyboard & keen to use XIB, there is a programmatically way of implementing those 2 Controllers as well. I'll discuss about them later in this tutorial series.
 
Dilum BC Navanjana
[CIS 2010/2011]
Associate Software Engineer(iOS), 
Creation Application
dilum@creationapplication.com 


 

iOS Development Beginner Tutorial 2 - MVC in iOS Development

MVC is a strategy how to organise all the classes in your application. All the classes in a app has 3 camps. They are, 
  1. Model - Model is what is your program. Model is UI independent.
  2. Controller - How something draw in your screen is the Controller’s job.
  3. View - View is the building block of the UI. Stuff in your view is generic
To know MVC correct you have to study how those 3 elements communicate with each other. 
Let’s talk about Controller talks to the Model. Controller can directly talks with Model. In the above picture also you can see it clearly. It’s a full unrestricted access. As well as Controller also can talk directly to their View. It layout the user interface to the View to draw in the screen. You can see, there is a name called “Outlet” in the start of that arrow. When we have a property of a controller that point in to the View we call it Outlet.The Model and View should never speak to each other. Model is 100% UI independent. There is no way Model can talk to View. View want to go through Controller to talk to the Model.

View can communicate with the Controller in a blind way. They don’t know the class they talking to. It’s a structured way. Controller has a target & the View has a action. That action can only go the that target. The View sends the action when things happen in the UI. When the View has some complicated ways to communicate. The Controller sets itself as the View’s delegate on those occasions. In the above picture with the Yellow arrow you can see a delegate. Delegation also a blind communication way. And also Views do not own the data they display. When you see something in your screen, it’s not the View’s data. The data is owned by the Model & Controller give those data to the View from Model. To get those data from the Model, View has to use Data Source. Data Source is just a kind of delegate, it’s a specific delegate. In building iOS apps you have to use delegates & also data sources.

Can the Model communicate to the Controller? Good questions. But when we are building apps we have some situations that Model want to communicate to Controller. For example think about a online radio streaming app. When the stream gives data to the Model, Model wants to communicate with the Controller & want to send those data to the Controller. How do we do that communication? It’s similar to radio station broadcast. The mechanism for doing this in iOS is called Notification & KVO. So the model changes, it broadcast that change & Controller tune in to interesting stuff.



This is how MVC works in iOS app development. Hope this will be helpful for your knowledge.


Dilum BC Navanjana
[CIS 2010/2011]
Associate Software Engineer(iOS), 
Creation Application
dilum@creationapplication.com