Search code examples
iosobjective-cuitableviewnestedchildviewcontroller

Tapping on cell in childviewcontroller accessing tableview delegate of parent's parent view controller in a nested environment


I am in a weird scenario. I have created a nestedViewController in which clicking on a row in tableview launches a another nestedviewcontroller just below the header of the parent nestedview's section header by adjusting frames. Now, To add a different functionality to second nested view , I added a UIViewController (which has custom cells in a tableview) as childviewcontroller to it. Now , when I click on the cell of the viewController , somehow the TableView Delegate of first NestedViewController is getting called!!! However, the view that is present is the UIViewController that I added as childViewController to second NestedViewController.

The way I added UIViewController to Second NestedViewController is :

 DRProductWriteRatingViewController  *writeRatingVC = [[DRProductWriteRatingViewController alloc] initWithNibName:@"DRProductWriteRatingViewController" bundle:nil];
        writeRatingVC.productDict = [productDict mutableCopy];

        newVC = [[DRRatingNestedViewController alloc] initWithFrame:CGRectMake(0,0,320,[UIScreen mainScreen].applicationFrame.size.height-                                                                     COLLAPSED_HEADER_HEIGHT*self.depth) andEntity:childEntity andListing:_listing];
        newVC.category = self.category;
        //            self.depth = self.depth+1;
        dispatch_async(dispatch_get_main_queue(), ^{

            writeRatingVC.tableView.frame = CGRectMake(0,0,320,[UIScreen mainScreen].applicationFrame.size.height-                                                                     COLLAPSED_HEADER_HEIGHT*self.depth-40);
            [newVC.tableView removeFromSuperview];// removing tableview of nestedVC as it is irrelevant
            [newVC addChildViewController:writeRatingVC];
            [newVC.view addSubview:writeRatingVC.view];
            [writeRatingVC didMoveToParentViewController:newVC];
        });

Also the way I push second NestedViewController Over First one goes like this:

- (void)pushViewController:(UIViewController *)viewController {

//1. The current controller is going to be removed
//    [self willMoveToParentViewController:nil];

[viewController willMoveToParentViewController:self];
//2. The new controller is a new child of the container
[self addChildViewController:viewController];

//3. Setup the new controller's frame depending on the animation you want to obtain
CGRect containerRect = CGRectMake(0,  self.view.bounds.size.height ,
                                  320.,
                                  self.view.bounds.size.height - COLLAPSED_HEADER_HEIGHT*(self.depth));
viewController.view.frame = containerRect;
[self.view addSubview:viewController.view];
//    viewController.view.alpha = 0;


//assume that the new view controller is positioned at the bottom of the screen
viewController.view.transform = CGAffineTransformIdentity;
//    CGFloat travelDistance = self.view.bounds.size.height - COLLAPSED_HEADER_HEIGHT - NAVIGATION_BAR_HEIGHT;
CGFloat travelDistance = self.view.bounds.size.height - COLLAPSED_HEADER_HEIGHT - NAVIGATION_BAR_HEIGHT;

if(self.depth>1) {
    travelDistance += NAVIGATION_BAR_HEIGHT;
}

travelDistance += NAVIGATION_BAR_HEIGHT;

if (self.depth >= 2) {
    travelDistance -=NAVIGATION_BAR_HEIGHT;
}

if (self.depth == 5 ) {
    travelDistance -=NAVIGATION_BAR_HEIGHT;
}
CGAffineTransform travel = CGAffineTransformMakeTranslation ( 0, -travelDistance);

[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:kDamping initialSpringVelocity:kInitialSpringVelocity options:0x00 animations:^{
    viewController.view.transform = travel;
    viewController.view.alpha = 1;
} completion:^(BOOL finished) {
    //      self.view.transform = CGAffineTransformIdentity;
    [viewController didMoveToParentViewController:self];
    //mayanktest        [self.tableView reloadData];
}];

}

The Depth here only decides the number of NestedVCs added.

First Nested VC: DRRatingNestedViewController: 0xd3a9520
Second Nested VC: DRRatingNestedViewController: 0xd0b4f50
Child View to Second Nested VC : DRProductWriteRatingViewController: 0xd0c15b0

So the tableViewDelegate with object 0xd3a9520 is called , which is my problem.

What should be the issue here?


Solution

  • The issue was with the childviewcontroller frame. Since the frame was smaller it was able to access the view behind. Hence, Modifications had to be done in this:

    CGRect containerRect = CGRectMake(0,  self.view.bounds.size.height ,
                                  320.,
                                  self.view.bounds.size.height - COLLAPSED_HEADER_HEIGHT*(self.depth));
    viewController.view.frame = containerRect;
    [self.view addSubview:viewController.view];