开发者

MVC with web queries

I've written my first app that uses web data for building a custom view. While it is working, I can't help but think that maybe it isn't quite following the MVC concept (thought I'm not sure this plays into how Apple approves apps or开发者_StackOverflow社区 not).

I have a single view controller with a custom view class that implements drawRect. Because the drawRect requires data from the web, it felt natural to do all the downloading within the custom view class itself.

But how is this usually done? I am guessing that the view controller should usually handle the downloading, and that the controller is the delegate for the async download so it can arrange views and such based on errors, etc. Instead, my custom view class is the delegate for handling the async, and that just seems poorly organized to me; but maybe it isn't?

If I were to instead use the View Controller for all the downloading, I suppose I would just set instance variables of the custom view to the results of the download, since the custom view needs the data to draw. Would that be a better approach?


Apple doesn't look at your source code as part of the App Store review process, so how you design your classes is up to you. However, you're right in thinking that in an MVC architecture, views don't fetch their own data, and in fact never have any knowledge of where the data they're presenting comes from. The controller layer acts as the bridge between views and model objects, so thats where the responsibility for fetching the data properly lies.

So I think you're on the right track: use an instance of a subclass of UIViewController to do obtain the data. From there, it's up to you to decide whether it makes more sense to 'push' or 'pull' the data into the UIView subclass you're designing.

In the push model, the controller sets instance variables of the view as necessary to present data. In the pull model, the view would typically send messages to the controller to request data prior to drawing. The typical pattern for this in iOS is to have the controller adopt a delegate protocol declared in the header for your custom view class. The view would then try to obtain data when needed by first checking to see whether its delegate implements the necessary methods, and then calling the methods if available.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜