I’ve created a navigationController application and it has within the stack a subclass which basically looks like this:
- rootViewController
- leaderboardViewController <—– UITableView within this VC set to files owner
- UITableViewCell
- UICollectionView <—– this is where I’m trying to push a new view controller
- UITableViewCell
Inside the collectionView didSelect method which is in the
UITableViewCell.m file:
- (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];
}];
}
then in the leaderBoardViewController.m is the postInfo method:
-(void)postInfo
{
NSLog(@"posted!");
largeInsight *detailedInsight = [[largeInsight alloc] initWithNibName:@"largeInsight" bundle:[NSBundle mainBundle]];
[[self navigationController] pushViewController:detailedInsight animated:YES];
}
the NSLog appears in the debug area so the postInfo function is running but it just doesn’t push the new viewController. I’m guessing it’s something I’ve missed or I’m doing something I’m not supposed to… I don’t think its because of the UICollectionView subclass as I’m referring back to the leaderboardViewController. I just don’t know why it’s not running that navigationController bit of code. Has anyone come across this same problem? Thanks!
UPDATE
with some help, I’ve removed the new instance variable of leaderboardViewController and put in NSNotificationCenter that points to the VC to run the postInfo method when the notification is sent:
- (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];
}];
}
then in leadboardVC.m where the collectionView is initialised:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postInfo) name:@"PostCellInformation" object:nil];
This line…
…creates a new instance of
leaderboardViewController. Since it’s new, it’s not part of the current view controller hierarchy that’s controlling the display. That means that anything it pushes is also not part of the current display hierarchy.(Because of that, I suspect your
navigationControllerisnilinpostInfotoo but I’d want to log that value to be sure.)