开发者

When to subclass UIViewController for custom subview?

开发者 https://www.devze.com 2023-02-19 21:34 出处:网络
I\'ve seen custom subviews implemented as an UIViewController subclass, but that maybe coul开发者_开发知识库d have been implemented as an UIView subclass.

I've seen custom subviews implemented as an UIViewController subclass, but that maybe coul开发者_开发知识库d have been implemented as an UIView subclass.

When should I subclass UIViewController instead of UIView for a subview? Are there any drawbacks to subclassing UIViewController?


Personally, when I need some significant logic to go on, I do it with a UIViewController subclass. Also, if I am looking for some of the behavior that you get from UIViewController e.g. presenting it modally or in a navigation controller.

If you are doing something fairly simple or lightweight, a UIView subclass is usually enough. I seem to have used them most often when making custom buttons and table view cells.

In my experience I have found myself using more UIViewController subclasses than UIView subclasses, but this might not be the best, it just so happens that I feel a bit more comfortable using view controllers rather than straight-up views.


Take a look at what Apple has to say on Controller Objects and the MVC design pattern

In iOS controller are generally expected to fill at least one the following roles:

  1. Coordinating controllers provide application specific logic. They respond to delegate messages, notifications, and IBActions. Coordinating controllers also setup connections between other objects and often manage the creation and destruction of those objects.

  2. View controllers, specifically UIViewControllers, manage the display of one "screen" worth of content and trigger transitions to the next "screen". They respond to memory warnings and rotation events.

  3. Mediating controllers exist in OS X but their role is usually filled by view controllers in iOS. They act as an intermediary between views and models; updating models when views receive input and updating views when models change.

If the behavior you are implementing fits into one of these categories you probably want to create a controller object. If your logic is only concerned with the display of data (and possibly responding to user input) then perhaps it belongs in the view layer. If your logic is about the data itself then it probably belongs in the model. If you can't find a good fit for your logic in any of those layers then you probably should model it differently as a combination of different responsibilities which belong on different objects in different layers. ie a view which requests data to display from a mediating view controller.


You would also subclass the UIViewController if you're going to use an AdBannerView in your "view". AdBannerView needs a UIViewController to be able to work.


The thumb rule I follow is, If you are doing custom drawing, subclass UIView. Otherwise, subclass the UIViewController.

0

精彩评论

暂无评论...
验证码 换一张
取 消