Let’s say you are developing an ipad application, and in 1 page you want to display different data.
At the bottom of the page you want to display some information that you want to keep separated from the rest of the page?
Questions:
- Would you use a UIViewController or a subclass of UIView for that portion of the page?
- Is it a bad practice to have too much logic in a UIView?
I’m asking this question because I ran into a lot of problem while unit testing my code. A UIViewController is easily testable since it does not require the UIView to be loaded, but a UIView is not testable unless it’s completely loaded (in my case loaded from a nib file)
Here’s my two cents
1) First, I would try to use one of those fancy iPad-only controllers (in particular, the split view controller). Alternatively, I would use a custom UIViewController with separate views for the two sections you want.
2) Oh man, this is a huge question: Is it a bad practice to have too much logic in a UIView? You must always use object oriented carefully; split up your design into classes and use inheritance/composition properly to avoid unmanageable code. Don’t forget that apple kindly helps you do this by being MVC friendly.
First of all, it’s bad practice to have the wrong kind of logic in a UIView. If you end up storing state that should belong on the model, you’ll be planting a few trouble seeds. Just don’t do it. The issue here is not how much logic, but the type of logic… remember that views are meant to be used by the controller and keep that in mind while designing them.
Second of all, it’s ok for your views to be complex as long as they have a specific purpose. Apple’s UITableView is an example of a very complex view, but using it is relatively straight forward because it was (in my opinion) designed correctly. Note that the tableview does not store any data… it gets it all from the datasource. Furthermore, it’s purpose is to display data in cells. If you emulate these good practices, complex views can still be manageable.
Finally, if you do decide to make a complex view, use composition to your benefit. I could go on and on, but the short version is that lots of logic is easier to manage if you use oop to your advantage and keep your code organized.