Search code examples
iosobjective-cipaduinavigationcontrolleruipopovercontroller

Navigation inside a UIPopoverController


I have a UIPopoverController that consist of table view. This pop over controller displayed well, and I already set the delegate didSelectRowAtIndexPath just fine.

Right now, I want to make some transition into "detail view controller" based on table item clicked. Then on destination view, it has back button like a pushViewController but it doesn't work well. It wont navigate into detail view Controller. This is my didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    DetailSummaryViewController *detailVC = [[DetailSummaryViewController alloc] initWithNibName:@"DetailSummaryViewController" bundle:nil];
    [self.navigationController pushViewController:detailVC animated:YES];
}

This is my popupover method

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CalendarCell *cell = (CalendarCell *)[collectionView cellForItemAtIndexPath:indexPath];

    UIPopoverController *popC = [[UIPopoverController alloc] initWithContentViewController:[SummaryViewController new]];
    [popC setPopoverContentSize:CGSizeMake(320, 400)];
    [self setPop:popC];

    [[self pop] presentPopoverFromRect:[cell frame]
                                inView:collectionView
              permittedArrowDirections:UIPopoverArrowDirectionAny
                              animated:YES];
}

Those navigation wont work, but if I NSLog-ing selected index it works nicely. Is there some step at setting up navigation that I am missed?


Solution

  • You donot have a Navigation Controller in your popover controller, so the method self.navigationController pushViewController wont work. Try this below:

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        CalendarCell *cell = (CalendarCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
        UINavigationController *insidePopoverNavigationController = [[UINavigationController alloc] initWithRootViewController:[SummaryViewController new]];
    
        UIPopoverController *popC = [[UIPopoverController alloc] initWithContentViewController:insidePopoverNavigationController];
        [popC setPopoverContentSize:CGSizeMake(320, 400)];
        [self setPop:popC];
    
        [[self pop] presentPopoverFromRect:[cell frame]
                                    inView:collectionView
                  permittedArrowDirections:UIPopoverArrowDirectionAny
                                  animated:YES];
    }
    

    Additional Credits: Raica Dumitru Cristian