3

Overview

  • I am doing an iOS project using the MVC architecture. Pardon my ignorance, I am just confused, I am looking for a good design.
  • The view I am planning to use will have some buttons on it and some labels and text fields. My view wouldn't need any custom implementation of drawRect.
  • All my logic as to what needs to be done when a button is pressed or event occurs is in my view controller

I have a couple of doubts:

  1. for the above scenario, is it still better (good practice) to create a separate view (a view other than view controller's view) ? If so why ?

  2. Other than drawing and displaying the view (in my project, I don't have much of it) what else should a view's implementation code contain ?

  3. I would like to disable a set of buttons when the user touches on a textfield and the keyboard comes up.

    a) So should I place this logic of disabling some buttons in the separate view's implementation (view created in question 1) ?

    b) From my parent view (view created in question 1), can I create outlets to the buttons (which are subviews) to disable some of the buttons ? I am not able to do this. Or should I use the method subviews and loop through the button that i am looking for ?

My understanding

  1. Model contains the data

  2. View is responsible for displaying and shouldn't contain business logic.

  3. View controller is the only one to interact between the model and the view and contains the business logic
user1046037
  • 16,755
  • 12
  • 92
  • 138

2 Answers2

4
  1. There's no need to create a separate view -- the view controller's view (usually just a plain UIView) can certainly contain your buttons and text fields. If you did want to put some of those in a separate container (perhaps so that you could move them as a group), you could use a plain old UIView for that.

  2. Views are responders, so UIView subclasses can override the touch handling methods if you want to do any special touch handling.

  3. a) It's common to put the code that manages views (such as disabling buttons) in the view controller. b) Again, you'd normally put the outlets for your buttons in the view controller.

When people talk about "business logic" they usually mean the logic that's required to maintain and operate on the data that the application deals with. That sort of thing is often best placed in the model. On the other hand, code that manages views, such as enabling or disabling buttons or moving data from the model into views (or vice versa) belongs in the view controller.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

Q1. for the above scenario, is it still better (good practice) to create a separate view (a view other than view controller's view) ? If so why ?

If you create your view by Interface Builder, that's a separate view I think. ;) But if you try to create a view hierarchy programmatically without using a n/xib, you can put all your view layouts in loadView method, and populate the data in viewDidLoad, that's what the View-Controller does. And also, you can create a UIView class to implement the layout of the view, just like n/xib, but programmatically.

As the DOC said,

... One can merge the MVC roles played by an object, making an object, for example, fulfill both the controller and view roles—in which case, it would be called a view controller. ...

... A view controller is a controller that concerns itself mostly with the view layer. It “owns” the interface (the views); its primary responsibilities are to manage the interface and communicate with the model. Action methods concerned with data displayed in a view are typically implemented in a view controller. An NSWindowController object (also part of the document architecture) is an example of a view controller. ...

The MVC in Cocoa is a litte different as what you known. You can refer the official doc HERE.


Q2. Other than drawing and displaying the view (in my project, I don't have much of it) what else should a view's implementation code contain ?

You can customize your view, e.g., set text color or font style for your button, etc.


Q3.a. So should I place this logic of disabling some buttons in the separate view's implementation (view created in question 1)

It is better to put logic in controller (or view-controller), just as MVC prefer.

Q3.b. From my parent view (view created in question 1), can I create outlets to the buttons (which are subviews) to disable some of the buttons ? I am not able to do this. Or should I use the method subviews and loop through the button that i am looking for ?

You can set tag (setTag:) for your buttons and get the right one you want. But keep in mind, firstly you need to let the button shown to parent.

Community
  • 1
  • 1
Kjuly
  • 34,476
  • 22
  • 104
  • 118