在嵌套环境中点击 childviewcontroller 中的单元格访问父级父级视图控制器的 tableview 委托

Posted

技术标签:

【中文标题】在嵌套环境中点击 childviewcontroller 中的单元格访问父级父级视图控制器的 tableview 委托【英文标题】:Tapping on cell in childviewcontroller accessing tableview delegate of parent's parent view controller in a nested environment 【发布时间】:2014-11-14 11:26:14 【问题描述】:

我处于一个奇怪的场景中。我创建了一个nestedViewController,其中单击tableview 中的一行通过调整框架在父嵌套视图的部分标题的标题下方启动另一个nestedviewcontroller。现在,为了向第二个嵌套视图添加不同的功能,我添加了一个 UIViewController(它在 tableview 中有自定义单元格)作为 childviewcontroller。现在,当我单击 viewController 的单元格时,不知何故第一个 NestedViewController 的 TableView Delegate 被调用了!!!但是,存在的视图是我作为 childViewController 添加到第二个 NestedViewController 的 UIViewController。

我将 UIViewController 添加到 Second NestedViewController 的方式是:

 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];
        );

我将第二个 NestedViewController 推到第一个之上的方式也是这样的:

- (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];
];


这里的深度只决定了添加的NestedVC的数量。

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

所以对象为 0xd3a9520 的 tableViewDelegate 被调用,这是我的问题。

这里应该是什么问题?

【问题讨论】:

【参考方案1】:

问题出在 childviewcontroller 框架上。由于框架较小,它能够访问后面的视图。因此, 必须在此进行修改:

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];

【讨论】:

以上是关于在嵌套环境中点击 childviewcontroller 中的单元格访问父级父级视图控制器的 tableview 委托的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap/iOS - 如何推送 childviewcontroller?

如何在全屏 ChildViewController 上显示 UIPageControl?

带有 UISegmentedControl 和 childViewController 的 iOS LargeTitle(在容器中)

将 ChildViewController 添加到 UIAlertController

ChildViewController 中的半滑不接收事件

从 ChildViewController 呈现 ModalView