6

So suppose you need some functionality that requires next storyboard. For example you need to upload different content to view depending on what tab is clicked. storyboard

But the problem comes out when you try to use this storyboard. When you switch tabs you getting this behaviour. bad behaviour

But in first tab everything fine. So looks like it doesn't load view second time. Can somebody explain or give a link to the behaviour of navigation controller in this case, because I can't find anything useful in reference. Or how should I correct this behaviour in IB or programatically?
Thanks.

htzfun
  • 1,231
  • 10
  • 41
  • You need to be more specific about your problem. Show us your code if you have done anything programmatically, and try debugging it yourself before asking. – Neeku Aug 05 '14 at 08:59
  • @Neeku I did nothing. It's just example that made only in storyboard and it doesn't work properly. – htzfun Aug 05 '14 at 09:01
  • You cannot make a UIViewController as the root view controller of two different Navigation controller. – Danyun Liu Aug 05 '14 at 09:07
  • @Danyun Could you explain me why? I'm a bit new in iOS and actually I don't get why second navigation bar can't create instance of UIViewController. It's not hierarchy of classes - it's just how views changes. I need to change content of the same view controller depending on what tab is clicked. I can't copy it, cause in real project i got bigger sequence of segues and views, so that it's impossible. – htzfun Aug 05 '14 at 09:13
  • I'm actually just about to file a radar on this to Apple. It bit me badly just because I had a complex way of getting to the same view controller two ways (one modally and one as a detail controller of a split view). Took forever to find out why navigation controllers were coming through in the segues with no child view controllers. Of course once I finally found the issue then I find this post on stack overflow. – user564904 Feb 20 '15 at 13:52

4 Answers4

2

a simple work-around is to put a "fake-viewcontroller" as root for the second navigation. On this "fake" controller execute in viewDidLoad a [self performSegueWithIdentifier: @"goToTheControllerHereWeGo" sender: self];

Mchurch
  • 74
  • 1
  • 8
1

So, as I mentioned in my comment I do think this is a bug but we'll see how Apple responds. But yeah, segues have no love for view controllers that are the root view controllers of multiple navigation controllers. There are a number of workarounds depending on the context under which it comes up.

Best workaround : Share the navigation controllers, not their root view controllers

So for the simple example given above you could do this and everything would be fine:

A UITabbarController with the same navigation controller for two tabs

Other workaround : This one is more applicable to complex storyboards that may have different custom navigation controllers so that sharing the nav controller isn't possible. One hilarious aspect of this issue is that when a view controller has two parent nav controllers in the storyboard, you won't know until runtime which one gets it! And further, on different runs they can switch :P (another reason I think this is a bug).

Sooooo from within prepareForSegue you can check if your navigation controller got unpacked with a rootViewController and, if it didn't, force it in there yourself:

UINavigationController* nc = segue.destinationViewController ;
if (nc.viewControllers.count == 0) {
    nc.viewControllers = @[[self.storyboard instantiateViewControllerWithIdentifier:@"MyDetailVC"]];
} 
user564904
  • 216
  • 2
  • 6
-1

Just provide more explanation beside the comments 'You cannot make a UIViewController as the root view controller of two different Navigation controller'. Suppose you can do so, then the view of the controller will be child view of the two navigation controller's view. It cannot happen as "it" cannot be a child of A but simultaneously a child of B.

Danyun Liu
  • 3,072
  • 24
  • 22
-1

On what condition will the tabview items switch , also trigger one of the two separate view controllers? what is the logic? when is it implemented? and no matter whatever the logic maybe, why does a single view controller (let us assume it is being filled up with different data according to the root) has 2 separate roots? we cannot add anything separately from the navigation controller itself, Navigation controller is the flow that sets the storyboard in motion, but putting a VC as a subview of two different NC just has no point.

Think it of like this, No additional information is provided by the Navigation controller itself, it just sets things in motion. So why would you want to put a VC as the child of 2root NC. More easily think it as multiple inheritance, in objc, java its not possible because of the Diamond problem. look it up and i hope it helps u understand

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
  • Your answer looks aggressive. Btw I know the Diamond problem and understand it but it's NOT a multiple inheritance, it's just relations. You write - whatever logic - person who buys wants this way - is it good reason? Enough, i think. I got tab bar and got nav bar and got the same controller in two cases of tab bar. So let's turn back to problem, not to the reasons. And about the Diamond problem - it's solved in C++, but not in Java. So it's not a good example. – htzfun Aug 05 '14 at 09:34