Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController in a UIView

Can I load a UiViewController in a UIView in another UIViewcontrller.

suppose UIViewControllerA has a UIView named subuiview. Can I load UIViewControllerB into subbuiview?

Thanks!

like image 538
nishantcm Avatar asked May 10 '11 11:05

nishantcm


People also ask

Is UIViewController a subclass of UIView?

Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy. A view controller's main responsibilities include the following: Updating the contents of the views, usually in response to changes to the underlying data.

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.

What is UIViewController in SwiftUI?

UIKit has a class called UIViewController , which is designed to hold all the code to bring views to life. Just like UIView , UIViewController has many subclasses that do different kinds of work. UIKit uses a design pattern called delegation to decide where work happens.

What is UIViewController life cycle?

The LifecycleThe view controller lifecycle can be divided into two big phases: the view loading and the view lifecycle. The view controller creates its view the first time the view is accessed, loading it with all the data it requires. This process is the view loading.


1 Answers

Starting with iOS 5

"Container view controllers" have been added in iOS 5. You can add a view controller as a child of another one with addChildViewController:.
You also are responsible for adding its view to its parent's view.

Everything is well documented in the iOS SDK documentation: Implementing a Custom Container View Controller.

To add a child view controller:

childViewController.frame = ...
[self.view addSubview:childViewController.view];
[self addChildViewController:childViewController];
[childViewController didMoveToParentViewController:self];

and to remove it:

[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];

Prior to iOS 5

It's possible to load another view controller and add its view as a subview of another controller's view.

UIViewController *subController = ...
[self.view addSubview:subController.view];

Although it's not recommended by Apple's guidelines:

Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy. [...] The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy.

(from the View Controller Programming Guide)

Your sub-controller won't receive rotation events, or viewWillAppear, viewWillDisappear, etc (except viewDidLoad).

So Apple advises us to use a single view controller managing the entire view hierarchy (but doesn't forbid to use multiple ones).

Each view may still be a custom subclass of UIView. Maybe you don't need another view controller but rather a custom view.

like image 72
Jilouc Avatar answered Nov 07 '22 02:11

Jilouc