When to use a UIView vs. a UIViewController on the iPhone?

前端 未结 9 738
谎友^
谎友^ 2020-12-04 16:25

I have always sort of wondered when to use a UIView vs. a UIViewController on the iPhone.

I understand that you shouldn\'t use a UIViewController unless it\'s a full

相关标签:
9条回答
  • 2020-12-04 16:48

    This is a great question.

    My basic rule of thumb. Is that every major 'page' of an application gets it's own view controller. What I mean by that is that during the wire framing stage of application design, everything that exists as its own entity will eventually be managed by its own View Controller. If there is a modal screen that slides over an existing screen, I will consider that to be a separate 'page' and give it its own view controller. If there is a view that overlays and existing page (such as a loading screen or help popup.) I would treat those differently, implement them as UIView subclasses and keep the logic in that 'pages' view controller. It the popup has behavior I will communicate back to that pages View Controller using the delegate pattern.

    I hope this helps. It is very much a philosophical and architectural question and much could be written about it.

    0 讨论(0)
  • 2020-12-04 16:54

    From Apple's View Controller Programming Guide for iOS:

    "The most important role of a view controller is to manage a hierarchy of views. Every view controller has a single root view that encloses all of the view controller’s content. To that root view, you add the views you need to display your content."

    Also:

    "There are two types of view controllers:

    • Content view controllers manage a discrete piece of your app’s content and are the main type of view controller that you create.
    • Container view controllers collect information from other view controllers (known as child view controllers) and present it in a way that facilitates navigation or presents the content of those view controllers differently.

    Most apps are a mixture of both types of view controllers."

    0 讨论(0)
  • 2020-12-04 16:54
    1. I use UIViewController for showing View on full Screen.

    2. For better control on custom view I prefer subclass of UIViewController instead of UIView, earlier I was using UIView for making custom sub class.

    0 讨论(0)
  • 2020-12-04 16:59

    I use UIViewController whenever a view is full screen and either has outlets/actions and/or subviews.

    0 讨论(0)
  • 2020-12-04 16:59

    Is the thing that slides in a self contained screen? I mean, does it directly interact with the parent? If so, make it a UIView, if not, probably recommend a UIViewController.

    0 讨论(0)
  • 2020-12-04 17:06

    I have a somewhat different approach:

    Override UIView if you plan to do custom drawing in drawRect. Otherwise, subclass UIViewController and use [self.view addSubview: blah] to add the components of the page.

    There are a few other special cases, but that handles about 95% of the situations.

    (You still will often need a UIViewController with a custom UIView. But it's common to have a custom UIViewController with no corresponding custom UIView.)

    0 讨论(0)
提交回复
热议问题