I’m trying to create a generic, reusable view, that looks like a lined notepad. The way I decided to approach the problem (after a couple design iterations) is to create a custom view that is composed of a UITextView and a UIView.
When the user scrolls through lines of text I want the UIView to track the scroll direction. The key here is: Within my custom view, I need to change the position of one subview in response to events in another subview. Something needs to coordinate these changes…
Now, one approach I thought of taking was to use a MVC design pattern. A view controller could handle all events and move the subviews around accordingly. This MVC could then be embedded in other MVCs.
Normally when using a MVC design pattern, a controller would handle user events and manipulate the model and view. However, my custom view doesn’t have a model – all I’m trying to do is have the view manage it’s own subviews when a user does something like scroll. It seems to me that the MVC design pattern isn’t a good fit here for two reasons:
- There isn’t a model or logic that is specific to the program it’s being used in.
- It seems to me that the view should be responsible for handling user events that change how the view should appear.
… but I could be wrong, which is why I’m asking for help. The question, for those who are more experienced than I and who may have done this many times before, is:
What type of design pattern is appropriate in this situation? MVC or…
You want a view to manage its own subviews? Then do that! So what if that pattern doesn’t have a TLA?
A typical approach is to implement
layoutSubviewsin your container view. Have it check its current state, or the state of the other views in the window (e.g. thecontentOffsetof a scroll view), and then set up its subviews appropriately. (Resize them, reposition them, etc.)Just try to keep it fast, since it’s likely that
layoutSubviewswill be called frequently.