Wanted to ask peoples opinion on this. I have a uiimageview subclass and in it's initializer I add some gesture recognizers to the imageview, and I also include the gesture recognizer delegate methods within class. My question is, does this violate model view controller? Should all the code that has to do with controlling a view be in a view controller? Same with say, putting a button action method in a view. Anyhow, interested in hearing opinions.
In one sense, yes, this violate the MVC pattern. As you say, the view shouldn't have anything to do with how to control it, it's a better habit to group such code in another part of the application.
Moreover, one of the great advantages of OOP is the ability to reuse classes. Views as such can be reused without any problem, as all the controlling code — specific to the application — is not in them. If you include the delegate methods in your view, you won't be able to reuse it, or you would have to change the delegate method each time!
But the rules are for the general case. Take a look at the Cocoa framework for Mac OS X : You've got the cocoa bindings which permit you to directly change a view content in response to a data change in your model. That does also violate the MVC pattern in some way.
One other example, see UIViewController. It must be a controller but it is so closely attached to a view that the question persists. The separation between view & controller is not as explicit as the MVC would like it.
To conclude, I would say that it's a good habit to follow the pattern (as long as they match your needs) and the way you can follow it using a given framework. But there's some case, if you make it explicit, where it could be interesting to create some shortcut and break the rules.
Seems like a perfectly good idea, if the action will only affect the appearance of the view. For example, attaching a pan gesture recognizer to a view to drag it around seems like a good idea.
However if it will trigger more wide-ranging effects in your application it's probably not the right approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With