2010-10-06 121 views
4

我已經添加了幾個按鈕,用下面的導航欄的右側:iPad的酥料餅presentpopoverfrombarbuttonitem

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

這種運作良好。對於這兩個按鈕,我表現出酥料餅如下圖所示

- (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,這顯示了兩個按鈕中間的頂部箭頭。我怎樣才能將箭頭附加到每個按鈕?的

回答

8

而不是使用此行:

aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem 

您可以更好地嘗試這一行:

aPopover presentPopoverFromBarButtonItem:sender 

我認爲這將解決您的問題

+0

非常感謝您的回答。出於某種原因,我不明白你真的必須使用發件人項目,並且不能使用你自己聲明的那個。 – guruz 2012-02-15 15:00:40

5

試試這個:

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

像一個工程魅力