Same root view navigation controllers

So, suppose you need some kind of functionality that requires the following storyboard. For example, you need to download other content to view, depending on which tab is clicked. storyboard

But the problem arises when you try to use this storyboard. When you switch tabs, you get this behavior. bad behavior

But on the first tab, everything is fine. It looks like he is not loading the view a second time. Can someone explain or give a link to the behavior of the navigation controller in this case, because I can not find anything useful in the link. Or how can I fix this behavior in IB or programmatically?
Thanks.

+6
source share
4 answers

the simple job is to put “fake control” as root for the second navigation. On this "fake" controller, do in viewDidLoad [self performSegueWithIdentifier: @ "goToTheControllerHereWeGo" sender: self];

+2
source

So, as I mentioned in my comment, I think this is a mistake, but we will see how Apple reacts. But yes, segues do not like view controllers, which are the root view controllers of multiple navigation controllers. There are a number of workarounds depending on the context under which it occurs.

Best workaround: Share navigation controllers, not root view controllers.

So, for the simple example above, you can do this and everything will be fine:

A UITabbarController with the same navigation controller for two tabs

Another workaround: this is more applicable to complex storyboards that may have different user navigation controllers, so sharing a navigation controller is not possible. One fun aspect of this problem is that when the view controller has two parent navigation controllers in the storyboard, you won’t know until it succeeds! And then, on different launches, they can switch: P (another reason, I think, is a mistake).

Sooooo, from inside prepareForSegue, you can check if your navigation controller has been unpacked using rootViewController and, if it hasn’t, do it yourself there:

UINavigationController* nc = segue.destinationViewController ; if (nc.viewControllers.count == 0) { nc.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"MyDetailVC"]]; } 
+1
source

Just give further clarification to the comments: "You cannot make the UIViewController as the root controller of two different navigation controllers." Suppose you can do this, then the controller view will be a view for two kinds of navigation controller. This cannot happen because “this” cannot be a child of A, but is also a child of B.

-1
source

In what state will the tabs switch, will one of two separate view controllers also start? What is the logic? when will it be implemented? and, regardless of the logic, why does one view controller (for example, it is populated with different data in accordance with the root) have 2 separate roots? we can’t add anything separately from the navigation controller itself. The navigation controller is a stream that sets the storyboard in motion, but turning VC on as a subtask of two different NCs just doesn't make sense.

Think about it. No additional information is provided by the navigation controller itself, it simply takes effect. So why do you want to put VC as a child from 2root NC. It is easier to consider this as multiple inheritance, in objc, java it is impossible because of the problem with diamonds. Take a look and I hope this helps you understand.

-1
source

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


All Articles