Tuesday, 5 February 2013

Cocoa design patterns and MVC


Cocoa design patterns
                  in order to be comfort using IOS SDK we must grasp the design behind cocoa objects. cocoa design has some common pattern you saw before in other languages like  
 MVC and other patterns inherited from the old NeXT "NextStep" framework  (from which we use the prefix NS in the cocoa core foundation framework). some of this patterns looks like a new concepts to someone come from c++ and c#; patterns like customization by delegates, messaging passing protocols and shared object ownership

In the coming posts i will explain some of the "every day use" patterns, that any IOS developer must get used to theses design pattern

In this post i will start with MVC design pattern

what is MVC ?

First letters of the three words Model, View and Controller 

in short model responsible of representing data, view for the interfacing with the user and controller to connect between them 
    • model
      • objects that used as a source or repository of data, data can come from memory like array, external databases or web service stream
      • it has data but don't know how to show it
    • view
      • these are the objects concerned with interfaces and the way the data look
      • it know how to draw data and how to present it but don't have something to show by itself
    • controller
      • this is our hero, it will connect the model with the view object. it can do the logical processes on the data from model and tell the view object "please show this by your way"
      • most of time you will need a controller along with view objects, and this is common in IOS programming
        • examples
          • UIViewController
          • UITableViewController
        • it has embedded UIview object inside it

Take this as example

we can do something like that 
[[currentViewController labelField] setText: [arrayOfObjects objectAtIndex:34]];

  • labelField: is the view object, which is a field, it could be table header or label on button
  • arrayOfObjects is the model, which has the actual data
  • currentViewController: make the connection by set the text of view to the object from the model which is the array
in the next post we will talk about delegates, another important IOS design pattern.  

Salam,
M.Aleem

No comments:

Post a Comment