Is it possible to add a UIViewController view to another UIViewController view?

Can I add a UIViewController view to another UIViewController view ?

Please explain why this is good practice or bad practice.

+4
source share
6 answers

Apple, and thus most people who adhere to Apple’s principles, will tell you about their bad practices, and Apple has added ViewController (childViewControllers) controls for this reason. Unfortunately, most people blindly follow this and will not tell you why this is bad practice. And I'm glad you ask for it.

The truth is that in the model-view-controller architecture, views must be reused regardless of the content they contain, so the view and the object that controls the content of the view should not be the same. This is exactly what the UIViewController does before iOS5. Apple does not encourage you to use them in animators, while this is very logical. Of course, this was incomprehensible, many people ignored the recommendations and still did it, including applications, the applications worked fine and passed the application store check, which caused even more confusion. As a result, people still ask questions about this even more than a year after Apple crashed and provided us with custom ViewControllers containers. I saw how people often answered this question with complex answers to recreate the UIViewController as a class that inherits NSObject for very simple problems. Just because Apple does not recommend using UIViewControllers and not even knowing why.

Since adding a ViewController view as a subquery will often work fine, and holding a ViewController is not available in iOS4, which many still support, too many people don't care about holding a ViewController. This is a cleaner solution, and when you want to use ViewControllers in ViewControllers, you should use it whenever possible. If not, in most situations you should just add the ViewController view as a subheading, you just need to know in which situations.

Here you can expect if you just add the ViewController view to another view:

  • Viewing callback methods is not guaranteed. Methods such as viewWillAppear, viewDidAppear, viewWillDisappear, and viewDidDisappear may or may not be called. This will largely depend on the OS version, in iOS4, which they will never call, in iOS5 and higher they will be mainly called. Thus, you cannot override these methods, because you cannot rely on them, you cannot control when, if or how many times they will be called.
  • The only callback method that will always be called correctly is viewDidLoad.
  • Rotation callbacks will not be called. Depending on your situation, this can be a big problem or no matter what. If the autoresizing view masks are enough to move and resize it, then you're fine. If not, you can always execute a custom implementation when the rotation callbacks of the viewview ViewController are called.
  • You must store the link to the ViewController yourself, otherwise it will be released immediately, while its view is still saved by its add-in.

I definitely won’t encourage him, but I don’t discourage him either. This is a situation, and if you no longer need to support iOS4, you can avoid it. But if you save the above list, it will not hurt, and your application will work fine.

+6
source

Sometimes this is normal, and sometimes not. It is difficult to give a better answer without displaying some diagrams and explaining the relationship between the view manager hierarchy and the view hierarchy.

Fortunately, Apple has already done this. Watch the video "Implementing the UIViewController Containment" from WWDC 2011 for a detailed explanation of when this is normal and when not.

+3
source

This is actually a common situation with complex hierarchies of representations. Since iOS 5, the UIViewController allows you to add a child view controller. When you add a child controller, you also add a child view to the controller view.

On the other hand, you should never add a view controller view to another view controller without adding it as a child view controller.

However, do not abuse it. You have to do it when

  • you implement a container for a set of controllers (something like your own UINavigationController or UISplitViewController )
  • child controller is independent. If child controllers constantly call methods for their parent and vice versa, it would be better to implement functionality in one controller.
+1
source

You can get away from it, but probably the best way. It seems reasonable to think that both will try to manipulate the point of view. My answer is no.

What are you trying to accomplish?

0
source

Of course, you can add a UIViewController view to another UiViewController view, at least as a class variable. But I cannot understand the ultimate goal of this decision. I think this is bad practice due to the complexity of the application interface.

0
source

Usually in the Model-View-Controller architecture, we can reuse views.

But for a UIViewController, this may not always be a good idea. This can lead to a complex project architecture because, according to the documentation, Apple is tightly attached to the view controller, so it can be easily managed.

From the UIViewController Help: View controllers are closely related to the views they control and participate in the responder chain used to process events. View controllers are descendants of the UIResponder class and are inserted into the responder chain between the managed root view and its superview, which usually belongs to another view controller. If the view of the view manager does not handle the event, the view controller has the ability to handle the event or can send the event to the supervisor.

But, I think, we can reuse the view if there are slight differences in the user interface for two different controllers.

0
source

Source: https://habr.com/ru/post/1397975/


All Articles