Presenting a UIPopoverController from UICollectionViewCell

半城伤御伤魂 提交于 2020-01-01 10:04:54

问题


I'm looking to present a UIPopoverController from a button on a UICollectionViewCell.

So far, everything is created ok, but the popover isn't visible.

Is there a special way of doing this?

The code works if I display it from anything else other than a collection view cell.

The following code is in the UICollectionViewCell subclass.

if (_infoPopover == nil) {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    GameInfoViewController *gameInfoVC = (GameInfoViewController *)[storyboard instantiateViewControllerWithIdentifier:@"GameInfoViewController_ID"];

    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:gameInfoVC];
    _infoPopover = popover;
    [gameInfoVC setGameNameString:_gameNameLabel.attributedText];
}

[_infoPopover presentPopoverFromRect:_infoButton.frame inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Thanks!


回答1:


Perform PopOver from UIViewController, not in UICollectionViewCell. So, use delegate to control.

//Cell.m
-(void)popOVerClick:(UIButton *)button{
    [[self delegate] didPopOverClickInCell:self];
}

implement protocol

//ViewController
    -(void)didPopOverClickInCell:(MyCell *)cell{
    if ([self.flipsidePopoverController isPopoverVisible]) {
        [self.flipsidePopoverController dismissPopoverAnimated:YES];
    } else {

        FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
        controller.label.text = cell.title;
        controller.delegate = self;

        self.flipsidePopoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
        [self.flipsidePopoverController presentPopoverFromRect:cell.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}

And the code for you: https://github.com/lequysang/TestPopOver




回答2:


change inView to collectionView

[_infoPopover presentPopoverFromRect:_infoButton.frame inView:self.collectionView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];



来源:https://stackoverflow.com/questions/14365654/presenting-a-uipopovercontroller-from-uicollectionviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!