6

我希望从 UICollectionViewCell 上的按钮呈现 UIPopoverController。

到目前为止,一切都创建好了,但弹出框不可见。

有没有一种特殊的方法可以做到这一点?

如果我从集合视图单元格以外的任何其他地方显示它,该代码就可以工作。

以下代码位于 UICollectionViewCell 子类中。

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];

谢谢!

4

2 回答 2

5

从 UIViewController 执行 PopOver,而不是在 UICollectionViewCell 中。所以,使用委托来控制。

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

执行协议

//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];
    }
}

和你的代码:https ://github.com/lequysang/TestPopOver

于 2013-01-21T17:09:18.860 回答
3

将 inView 更改为 collectionView

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

于 2013-01-23T03:09:45.030 回答