是否可以从子类中推送视图控制器?

时间:2012-12-05 13:10:55

标签: ios uitableview uinavigationcontroller subclass

我已经创建了一个navigationController应用程序,它在堆栈中有一个子类,它基本上是这样的:

  • RootViewController的
  • leaderboardViewController< -----此VC中的UITableView设置为文件所有者
    • 的UITableViewCell
      • UICollectionView< -----这是我试图推送新视图控制器的地方

在collectionView内的didSelect方法中 UITableViewCell.m文件:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    userCell *user = (userCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    imageTag = ((UICollectionView *)user).tag;
    leaderboardViewController *leaderboardView = [[leaderboardViewController alloc] init];
    [imageManager getInsightWithCompletionBlock:^(UIImage *image){
          [leaderboardView postInfo];

    }];
}

然后在leaderBoardViewController.m中是postInfo方法:

-(void)postInfo
{
    NSLog(@"posted!");
    largeInsight *detailedInsight = [[largeInsight alloc] initWithNibName:@"largeInsight" bundle:[NSBundle mainBundle]];
    [[self navigationController] pushViewController:detailedInsight animated:YES];        
}

NSLog出现在调试区域中,因此postInfo函数正在运行,但它不会推送新的viewController。我猜这是我错过的东西,或者我正在做的事我不应该...我不认为它是因为UICollectionView子类,因为我指的是leaderboardViewController。我只是不知道为什么它没有运行那个navigationController的代码。有没有人遇到同样的问题?谢谢!

更新

在一些帮助下,我删除了leaderboardViewController的新实例变量并放入NSNotificationCenter,指向VC,以便在发送通知时运行postInfo方法:< / p>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    userCell *user = (userCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    imageTag = ((UICollectionView *)user).tag;

    [imageManager getInsightWithCompletionBlock:^(UIImage *image){[[NSNotificationCenter defaultCenter] postNotificationName:@"PostCellInformation" object:self];
    }];
}

然后在leadboardVC.m中初始化collectionView:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postInfo) name:@"PostCellInformation" object:nil];

1 个答案:

答案 0 :(得分:1)

这一行...

leaderboardViewController *leaderboardView = [[leaderboardViewController alloc] init];

...创建leaderboardViewController实例。由于它是新的,它不是控制显示的当前视图控制器层次结构的一部分。这意味着它推送的任何内容也不是当前显示层次结构的一部分。

(因此,我怀疑navigationController中的nil postInfo也是{{1}},但我想记录该值以确定。)

相关问题