What's the Difference Between a View and a Viewcontroller

What is the difference between a View and a View Controller?

A view is an object that is drawn to the screen. It may also contain other views (subviews) that are inside it and move with it. Views can get touch events and tell listeners or delegates about any events that occur in them. Views are dumb, and do not know about the structure of your application, and are simply told to display themselves in some state, and listened to for events.

For example: a text label, a button, a checkbox, a progress bar.


A view controller is not drawable to the screen directly, it manages a group of view objects. View controllers usually have a single view with many subviews. The view controller manages the state of these views. A view controller is smart, and it knows how to interact with your application. It tells the dumb view objects what to do and how to show themselves.

For example: a screen that shows a list of users, an edit user form, a login screen.


A view controller is the glue between your overall application and the screen. It controls the views that it owns according to the logic of your application.

What's the difference between a View and a ViewController?

Take a look at this post - What is the difference between a View and a View Controller?

This described it pretty well for me.

If you don't want to go to the link, here is a great description of the difference between a view and a view controller by Alex Wayne:

A view is an object that is drawn to the screen. It may also contain
other views (subviews) that are inside it and move with it. Views can
get touch events and change their visual state in response. Views are
dumb, and do not know about the structure of your application, and are
simply told to display themselves in some state.

A view controller is not drawable to the screen directly, it manages a
group of view objects. View controllers usually have a single view
with many subviews. The view controller manages the state of these
views. A view controller is smart, and has knowledge of your
application's inner workings. It tells the dumb view objects what to
do and how to show themselves.

A view controller is the glue between your overall application and the
screen. It controls the views that it owns according to the logic of
your application.

Difference between navigation controller and viewcontroller?

Just my two cents:

A UIViewController represents a single view and you can put buttons in this view controller to segue to another UIViewController. If you want to segue back to the first UIViewController, you will have to worry about putting a button in the second view controller that leads back to the first. If you are drilling down into view controllers, this can be tedious having to remember to give the user a way back to a previous view controller.

A UINavigationController does a lot of this tedious work for you. As mentioned, it contains a stack of UIViewControllers. It will create a navigation bar at the top that will allow you to easily go back up the hierarchy of view controllers.

In short, if you have a hierarchy of view controllers that you want the user to easily navigate around, inbed your UIViewControllers into a UINavigation controller.

What is the difference between presenting a ViewController vs adding a ViewController as a child

The difference is that when you present a controller, it becomes modal, and when you add it as a child controller, it doesn't. The difference is in semantics and use cases.

Modal

As an example of modal window, imagine a window with an OK button in macOS that prevents you from interacting with the parent window until you close it.

Hence why there can be only one view controller presented by a particular controller: the presented controller becomes “main” for user interaction, and the user must close it to return to the parent window.

A typical example of a modal window is an alert.

Child

But a child view controller is a different story. There can be any amount of them on a particular VC.

The parent controller becomes sort of container for child view controllers. For example, there can be a dashboard with multiple panels, and each panel can be a separate controller. This allows to separate logic between different pieces of an application instead of cramming everything into a single controller, to implement user customization in easy way, etc.

A typical example of a parent-child controller interaction is UINavigationController: the navigation controller is the parent, and all view controllers pushed into it are its children. We don't make an uber controller with the whole application's logic, we separate it between different screens, and the navigation controller is only responsible for presentation.

What's the difference between UIViewController and ViewController?


What is the difference between these two ViewControllers?

Not a great deal, but it depends on the functionality defined in ViewController. It's fairly common to create a base class that contains common functionality you want in all derived classes, and view controller classes are no exception here.

However if ViewController contains no real functionality, then you can simply remove it and derive GameViewController directly from UIViewController:

#import <UIKit/UIKit.h>

@interface GameViewController : UIViewController

I would be very surprised if Xcode generated both ViewController and GameViewController in one operation, as you imply in your question, however. If it did, then that's new to me and I cannot see why it did it.

What is the difference between UIView and UIViewController?

The UIViewController is the controller part in the MVC design pattern.

Model<------->Controller<------->View

The controller's task is to handle navigation between different views, key presses, and screen touches etc.

Difference between table view controller and a table view placed within a viewcontroller

Use a UITableView. This a bit more customizable in terms of Storyboard layout. You can place UIImageViews, toolbars, and other elements all over your UIViewController. You can then put a UITableView in the exact place that would work for you, with the dimensions you need.

Of course, you could always use a UITableViewController. You could embed this controller in a variety of combinations, which would let you add tab bars or navigation bars.

The only real difference in implementation is that you have to remember to explicitly write the delegate and data source methods when using a UITableView.

For your case, I would pick whatever seems easiest to implement in your case. Probably a UITableView in my opinion.



Related Topics



Leave a reply



Submit