MVC: passing model pointer to view?

I have an iOS application running and I'm trying to clear part of the code structure and implementation. I would like to clarify my understanding of MVC and improve my code.

Question: is it possible to pass the model to UIView so that the view can display it based on the states of the model elements?

I will describe the example below, but I understand that it may be unclear, too tedious to read, etc. The bottom line is that if the UIView does not change any values ​​in the model, is it normal that it maintains a weak reference to the model? (say, compared to what I always asked my delegate to return a temporary pointer to the model).

- thanks for any comments!

Example: Imagine a UIView, which is a 10-story CONSTRUCTION with 1 WINDOW on each floor. The MODEL for this is an NSArray containing 10 instances of custom WINDOW objects. Every WINDOW obj. has a state (light on / off) and CGRect representing the position of the WINDOW in the rectangular rectangle of the building.

The BUILDING instance controller determines the size of the building view (its frame) and all WINDOW objects, including CGRects, state, etc., creating an NSArray MODEL. Then I assign this UIView MODEL to the BUILDING controller (but save it as a strong BUILDING controller property).

UIView needs to know the state of WINDOW and CGRect in order to draw a view in drawRect .

I think that I could store CGRs separately, since they are not abstract data, but it was easier to pack all this into a single array of objects.

+4
source share
2 answers

You are on the right way. But there is no reason for the idea to have a β€œweak” reference to the model. It is appropriate that he has a strong link to the part of the model that it actually displays, if you do not want the part of the model data to disappear during its display. That would be unusual.

Let's say I have this thing called WindowPaneView that displays a WindowPane (just avoiding the confusion with UIWindow here). There is nothing wrong with a controller creating a view and passing it a strong link to WindowPane . This is a very good design in many cases.

What would be wrong for WindowPaneView to make requests to the Building to get the correct window information. The controller must talk to Building , share information and pass each WindowPaneView its own Window .

It would also be wrong if WindowPane knew nothing about WindowPaneView . A model should never know about views. Madness quickly drops when this happens. But the views may, of course, be aware of the specific part of the model that they directly reflect. Just not anymore.

+4
source

The best choice will depend on the lifetime of the objects in question.

In the example that you are describing, your controller object should save both the model and the view, and then provide the view with a weak reference to the model. Then your controller is responsible for all memory management and can properly manage the view until the model is released.

If your controller does not own the model object (which can easily happen in an application with many different interacting controller objects), you have two options:

  • Keep strong link to NSArray
  • Hold a copy (possibly a deep copy) of the NSArray that the NSView uses as long as necessary.

In general, I would be inclined to option 2, because it eliminates the need for any long-term problems with memory management.

+1
source

Source: https://habr.com/ru/post/1398735/


All Articles