Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting a UIPopoverController from UICollectionViewCell

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!

like image 268
W Dyson Avatar asked Jan 16 '13 18:01

W Dyson


1 Answers

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

like image 107
LE SANG Avatar answered Oct 29 '22 07:10

LE SANG