Discuss on MVC implementation on iPhone

后端 未结 4 1001
刺人心
刺人心 2021-02-04 16:52

Im a using the MVC pattern for a while on different frameworks such as (swing, android, gwt ...) Now, I\'m learning the iPhone framework and I am quite surprised about MVC imple

4条回答
  •  广开言路
    2021-02-04 17:23

    The iPhone ViewController is intended to manage a collection of views, all of which combine into a coherent interface. For example, a view controller may manage a scroll view with a dozen label views, a switch view, and two button views. Each UIView-derived piece handles the on-screen drawing of a specific element -- a button, a scroll layer, a label -- with the View Controller directing what data gets put where. So, a UIViewController-derived class focuses on the presentation (more View in the MVC sense), by marshaling data into the right UIView objects (like a Controller.) I think ViewController is an apt term for it.

    Sometimes to managed a single screen, a UIViewController will have several other UIViewControllers for specific parts of the display. The UINavigationController, for example, maintains a stack of UIViewControllers each of which handle one page of the display. Cocoa is all about layering.

    A UIViewController may and often does have a delegate (and sometimes a data source) that it uses to ask what data belongs in the views and to filter up user input. In my mind, that makes the delegates closer to the MVC Controllers because they are the classes that manage all the models and include any application (business) logic. The AppDelegate is the big one, in charge of managing the overall state of the application. The AppDelegate may keep track of all the data or it may dole out parts of the data to other delegates. I would expect the Facebook app to have one delegate for managing all the Events, another for Wall posts and a third for Photos, each of which report up to the AppDelegate for communicating with Facebook servers.

    When I'm writing my apps, I tend to use CoreData for the MVC Model, Delegates for the MVC Controllers and leave the MVC Views to UIViewControllers tasked with wrangling the herds of UIViews.

提交回复
热议问题