我正在寻找从 UICollectionViewCell 上的按钮呈现 UIPopoverController。
到目前为止,一切都创建好了,但弹出框不可见。
有没有一种特殊的方法可以做到这一点?
如果我从 Collection View 单元格以外的任何其他地方显示代码,则该代码有效。
以下代码位于 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];
谢谢!
最佳答案
从 UIViewController 执行 PopOver,而不是在 UICollectionViewCell 中。所以,使用委托(delegate)来控制。
//Cell.m
-(void)popOVerClick:(UIButton *)button{
[[self delegate] didPopOverClickInCell:self];
}
执行协议(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];
}
}
和你的代码:https://github.com/lequysang/TestPopOver
关于xcode - 从 UICollectionViewCell 呈现一个 UIPopoverController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14365654/