iPad popover presentpopoverfrombarbuttonitem

Posted

技术标签:

【中文标题】iPad popover presentpopoverfrombarbuttonitem【英文标题】: 【发布时间】:2010-10-06 03:00:36 【问题描述】:

我在导航栏的右侧添加了几个按钮,如下所示:

UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
customView.backgroundColor = [UIColor clearColor];

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,  0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:@"toc.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:@selector(tableOfContentsAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];

button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:@"bookmark.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:@selector(bookmarkButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:customView];

self.navigationItem.rightBarButtonItem = segmentBarItem;
[customView release];
[segmentBarItem release];

这很好用。对于这两个按钮,我都会显示一个 popOver,如下所示

- (void) bookmarkButtonAction

BookmarksViewController* content = [[BookmarksViewController alloc] initWithOrientation:lastOrientation selectedPage:selectedPage];
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:content];
CGSize size = content.view.frame.size;
aPopover.popoverContentSize = size;
aPopover.delegate = self;
self.bookmarksPopoverVC = content;
self.bookmarksPopoverVC.popUpController = aPopover;
[content release];
[aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[aPopover release];
bookmarksShowing = YES;

问题是我正在使用 presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem,这显示了两个按钮中间的顶部箭头。如何将箭头附加到每个按钮?

【问题讨论】:

【参考方案1】:

而不是使用这一行:

aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem

你最好试试这条线:

aPopover presentPopoverFromBarButtonItem:sender

我认为这会解决你的问题

【讨论】:

非常感谢您的回答。出于某种原因,我不明白你真的必须使用发件人项目并且不能使用你自己声明的那个。【参考方案2】:

试试这个:

- (IBAction)products:(id)sender 
    UIButton* btn = (UIButton *)sender;
    [productsPopover presentPopoverFromRect:[btn bounds] inView:btn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

像魅力一样工作

【讨论】:

以上是关于iPad popover presentpopoverfrombarbuttonitem的主要内容,如果未能解决你的问题,请参考以下文章

iPad:iOS5 的 Popover 示例

带有 PopOver 控件的 IPad 按钮下拉菜单

如何在 ipad 中创建 Popover? [关闭]

在 iPad 的同一视图中加载多个 popOver

iPad popover presentpopoverfrombarbuttonitem

iOS_21团购_Popover适应iPad横竖屏切换