Tuesday, June 24, 2014

iOS Tutorial - Part 12 - View and Controller communication, View Lifecycle

View and Controller communications, View Lifecycle 

 

Video Description 

View and Controller communication

In the Model View Controller design pattern we have communications between View and Controller. If Controller wants to send message to the view, it uses IBOutlet. If View wants to send message to the controller that for example some button pressed or the slider has changed it uses IBAction.

View Lifecycle

A series of methods that are sent to the View Controller when things happened. For example if we want to initialize some parameters before view appears to the user, we should use view lifecycle methods. Using these methods means overriding them. Because we override these methods, the first line of each method we should call the super (super class) and the name of the method. Here are the important view lifecycle methods:

viewDidLoad

The most important view lifecycle method is this method. This is a great place for initialization because it will be called only one time during the whole lifecycle and also all outlets are set. But do not initialize things that are related to geometry because at this point we can't be sure if we are on iPhone 5 size or 4 or iPad ...
- (void)viewDidLoad
{
    [super viewDidLoad]; //Always add this line as the first line of code for this method
    //Do initialization
}

viewWillAppear

This method is called each time that the view appears to the screen so it's not a good place for initialization because it could be called more than one time during the lifecycle. But it's a good place for updating the view.
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //Do updating the view
}

viewWillDisappear

This will be called a moment before view disappear from the screen. This is a good place to save the data to the storage. For example remembering the scroll position or saving what user typed so far, in order to retrieve it once s/he comes back to the view.
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //Save Data
}

4 comments:

  1. What is difference between Core Data and SQLite.?

    ReplyDelete
    Replies
    1. For more info visit:
      http://www.cocoawithlove.com/2010/02/differences-between-core-data-and.html

      Delete
    2. SQLite is a database itself like we have MS SQL Server. But Core Data is an ORM (Object Relational Model) which creates a layer between the database and the UI. It speeds-up the process of interaction as we don't have to write queries, just work with the ORM and let ORM handles the back-end.

      For save or retrieval of large data, I recommend to use Core Data because of its abilities to handle the less processing speed of iOS devices. One cool thing about Core Data is that you can access the data with dot notation, for example if you have a table called Photographer and inside of this table you have an entity called photo, you can access it like photographer.photo.

      I will have a full tutorial for Core Data. Thanks for visiting our website.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete