4

I am developing an iPad application which has a single view controller (called as ContentViewController) with 3 different views in it.

  1. Slider view - opens from bottom, which has list of icons in it. Based on selecting a icon, I have to load a view controller in Content View
  2. Control view - left side of the screen with few buttons and text
  3. Container view - This covers large part of the screen, where I want to load view controllers based on the selection of an icon from slider

This is how I implemented it

At application start (for the first time), I normally load home view controller in Container View, which has table view with application related items. Every view controller is inside a navigation controller and I load that navigation controller in the container view

When I select a icon in the slider view, I am loading a view controller.

The following is the code that I implemented to do this stuff in a view controller with name ContentViewController:

- (void) itemSelected: (UIViewController *) viewController
{
   // I am storing view controller in a instance variable currentViewController. The currentViewController is declared as @property (nonatomic , strong) UIViewController *currentViewController under @interface in header file
   if(_currentViewController == nil)
   {
      // This part of code gets executed for the first time, when there is no view controller available in ContainerView
      _currentViewController = viewController;
      [self addChildViewController:_currentViewController];
      [self.containerView addSubview:_currentViewController.view];
   }
   else if(_currentViewController != viewController)
   {
       // If a view controller is already opened in Container View and when I click a icon from the slider, this par of code is getting executed
       [self addChildViewController:viewController];
       [self transitionFromViewController:_currentViewController
                      toViewController:viewController
                              duration:0
                               options:UIViewAnimationOptionTransitionNone
                            animations:^{}
                            completion:^(BOOL finished){
                                [_currentViewController removeFromParentViewController];
                                _currentViewController = viewController;
                                [_currentViewController   didMoveToParentViewController:self];
                            }
     ];        
    }
}

The code mentioned above is working fine in iPad2 and iPad3, which are 32 bit device. But when I run this application on iPad Air (64 bit device), it is crashing in transitionFromViewController throwing following error:

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UINavigationController: 0x136d76130> should have parent view controller:(null) but actual parent is:<ContentViewController: 0x136d39680>'
*** First throw call stack:
(0x183642950 0x19001c1fc 0x183642890 0x186688f00 0x18661484c 0x186613ff4 0x10009a224 0x1001104c8 0x18673d798 0x1867fe234 0x186692470 0x1865fe4a4 0x1836030a8 0x183600330 0x1836006bc 0x1835416d0 0x1891ddc0c 0x186672fdc 0x100058028 0x19060faa0)
libc++abi.dylib: terminating with uncaught exception of type NSException

I tried various options like removing transitionFromViewController and replacing with following code:

    [_currentViewController willMoveToParentViewController:nil];
    [_currentViewController removeFromParentViewController];
    _currentViewController = firstView;
    [_currentViewController didMoveToParentViewController:self];

    [self addChildViewController:_currentViewController];
    [self.containerView addSubview:_currentViewController.view];

But it again crashed in last line [self.containerView addSubview....] with same error mentioned above in iPad Air. I am not sure how to proceed and I don't why this issue happens only with 64 bit device. Could someone please help me on this.

Thanks in advance!

Vignesh

Vignesh
  • 145
  • 1
  • 12

1 Answers1

3

I am able to resolve this issue by changing the code. In the previous code, I was adding the view controller as child view controller and removed previous view controller from parent view. That code was working fine in non-64 bit device, but not on 64 bit device. So I just added view controller's view into subview of the container view and removed previous view controller from super view. Here is the modified code for reference:

if(_currentViewController == nil)
{
    _currentViewController = viewController;
    [self.containerView addSubview:viewController.view];
}
else if(_currentViewController != viewController)
{
    [_currentViewController.view removeFromSuperview];
    [self.containerView addSubview:viewController.view];
    _currentViewController = viewController;
}

It works fine on all device.

Vignesh
  • 145
  • 1
  • 12