Navigation inside a UIPopoverController

2019-08-08 19:27发布

问题:

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?

回答1:

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



回答2:

when you create the UIPopoverController, instead of setting the MyViewController inside the UIPopoverController, you should set a UINavigationController

    UINavigationController *insidePopoverNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
    popoverController = [[UIPopoverController alloc] initWithContentViewController:insidePopoverNavigationController];
    ...... 
    [popoverController presentPopoverFromRect:... etc];