3

I am at the beginning of developing an iOS app I am having trouble understanding the MVC (Model-View-Controller) design pattern. I thought I had it down but the more I read the more confused I get. I don't know if this should be an iOS specific question or if the same goes for every use of MVC. I am not using storyboards btw, I want to do it all programmatically.

I believe I understand the basic relationship between Controllers and Models, it's the separation of Views and Controllers that I don't get. Let's say I want to create a UIButton and display it on the screen. Should I initiate the button in the Controller or the View? The controller is responsible for what should be displayed, correct? Wouldn't you just call on a View to display that and not worry about creating the button in the Controller? From what I understand, View Controllers are just Controllers and should therefore control the View, not be the View. It seems like most people do just about everything in the View Controllers. I guess my question boils down to what code goes where?

John Topley
  • 113,588
  • 46
  • 195
  • 237
rooskist
  • 39
  • 4
  • Apple has a doc for that: https://developer.apple.com/library/ios/documentation/general/conceptual/devpedia-cocoacore/MVC.html – Tim Reddy Feb 18 '14 at 14:33
  • I have 2 guide suggestions for you: [Model-View-Controller](https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Model-View-Controller/Model-View-Controller.html#//apple_ref/doc/uid/TP40010810-CH14) and [View Controller Programming Guide for iOS](https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html) – Desdenova Feb 18 '14 at 14:35
  • Note: "view controller" is not a part of MVC pattern. It's something specific for that framework. – tereško Feb 19 '14 at 08:48

3 Answers3

1

UIViewController is controller part in the MVC pattern of code.

  • M(Model)
  • C(ontroller)
  • V(View)

Controllers handle navigation between different views , etc.

From here you can get more idea : view and viewcontroller

A view is an object that is drawn to the screen. It may also contain other views (subviews) that are inside it and move with it. Views can get touch events and change their visual state in response. Views are dumb, and do not know about the structure of your application, and are simply told to display themselves in some state.

A view controller is not drawable to the screen directly, it manages a group of view objects. View controllers usually have a single view with many subviews. The view controller manages the state of these views. A view controller is smart, and has knowledge of your application's inner workings. It tells the dumb view objects what to do and how to show themselves. A view controller is the glue between you overall application and the screen. It controls the views that it owns according to the logic of your application

About View Controllers View controllers are a vital link between an app’s data and its visual appearance. Whenever an iOS app displays a user interface, the displayed content is managed by a view controller or a group of view controllers coordinating with each other. Therefore, view controllers provide the skeletal framework on which you build your apps. iOS provides many built-in view controller classes to support standard user interface pieces, such as navigation and tab bars. As part of developing an app, you also implement one or more custom controllers to display the content specific to your app.

Community
  • 1
  • 1
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
0

Not sure about any iOS specifics but from a PHP standpoint controllers are there to get any data that is needed for the view (via models) and then pass that data to the view.

The view is there to render things to the screen only and should have no logic in it.

So from a website point of view if you wanted to display a users name on the screen:

The controller would request the username from the model The model would get the username and return it to the controller The controller would then pass the username to the view And the view would then render the username.

Hope that helps.

Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
0

To be not specific, but on a upper layer, you can say as following :

You can put controller code in class extending UIViewControllers

You can put view code in class extending UIView

You can put model code in class extending NSObject

Samkit Jain
  • 2,523
  • 16
  • 33