I have a User
class has some properties such as name, email, location. And I have another class
called Post
which has some properties such as title
, content
and poster
that is a instance of User
.
There is a ViewController, inside the view controller there is a PostDetailView
which is used to show post details. Now the view controller has a post object and I need to pass some values to PostDetailView
. I have two options:
post
object through initializer, which means PostDetailView
has a method -initWithFrame:post. Once the view get the post object, it can get every data inside the post.PostDetailView
has some setter method such as setTitle:
, setContent
, setPosterName
, etc. Initializer only initialize frame of view, and then using setters to pass value.Option 1 can save lots of work in view controller but may increase coupling. Option 2 has better structure (I thought), but need additional work in both view and view controller. Therefore, my question is which one is better in terms of architecture?
In favor of good design and architecture it make sense to type a little more and have views decoupled from model. One technic that I've used to is to design view with properties of primitive types (String, Number etc.) and create a category for the view that has a method to configure itself with a model object of a particular type.
You can read more about this here: http://www.objc.io/issue-1/table-views.html They talk about table view but approach can be used for any view and model with mediating controller.