Understanding Apple's MVC pattern

Before getting too far with iOS development, it is really important to get a foundation with Apple's design pattern for developing on iOS. You may have used the MVC (Model View Controller) pattern with other technologies such as ASP.NET, but Apple implements this paradigm in a slightly different way.

The MVC design pattern includes the following:

  • Model: This is the backend business logic that drives the application. This can be any code that, for example, makes web requests to a server or saves data to a local SQLite database.
  • View: This is the actual user interface seen on the screen. In iOS terms, this is any class that derives from UIView. Examples are toolbars, buttons, and anything else the user would see on the screen and interact with.
  • Controller: This is the workhorse of the MVC pattern. The controller interacts with the model layer and updates the view layer with the results. Just like the view layer, any controller class would derive from UIViewController. This is where a good portion of the code in iOS applications resides.

The following image shows the MVC design pattern:

Understanding Apple's MVC pattern

To understand this pattern better, let's walk through the following example of a common scenario:

  1. We have an iOS application with a search box that needs to query a website for a list of jobs.
  2. The user will enter some text into the UITextField textbox and click on the UIButton button to start the search. This is the view layer.
  3. Some code will respond to the button by interacting with the view, display a UIActivityIndicatorView spinner, and call a method in another class to perform the search. This is the controller layer.
  4. A web request will be made in the called class and a list of jobs will be returned asynchronously. This is the model layer.
  5. The controller will then update the view with the list of jobs and hide the spinner.

Note

For more information on Apple's MVC pattern, see the documentation at https://developer.apple.com/library/mac/documentation/general/conceptual/devpedia-cocoacore/MVC.html.

A point to note is that you are free to do anything you want in the model layer of your application. This is where we can use plain C# classes that can be reused on other platforms such as Android. This includes any functionality using the C# Base Class Libraries (BCL), such as working with web services or a database. We'll dive deeper into cross-platform architecture and code-sharing concepts later in the book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.9.146