1

当点击 UIButton 以显示 UIPopovercontroller 时,它会显示在另一个 UIbarbuttonite 上,并且它会显示空白的黑色不透明 UIPopovercontroller。

这是我实现的代码:

- (IBAction)buttonTapped:(id)sender {

UIButton *btnAction;

BookmarkViewController* bookmarkVC = [[BookmarkViewController alloc] init];

_buttonPopoverController = [[UIPopoverController alloc]
                                initWithContentViewController:bookmarkVC];

_buttonPopoverController.delegate = self;

CGRect popoverFrame = btnAction.frame;
[_buttonPopoverController setPopoverContentSize:CGSizeMake(320, 355) animated:NO];

//only required if using delegate methods

[_buttonPopoverController presentPopoverFromRect:popoverFrame
                                              inView:self.view
                            permittedArrowDirections:UIPopoverArrowDirectionUp
                                            animated:YES];
}
4

3 回答 3

2
UIButton *btnAction=(UIButton*) sender;
于 2013-08-16T12:40:57.513 回答
1

你的问题在这里

[_buttonPopoverController presentPopoverFromRect:popoverFrame
                                              inView:self.view
                            permittedArrowDirections:UIPopoverArrowDirectionUp
                                            animated:YES];

presentPopoverFromRect:popoverFrame实际上应该是弹出框的矩形不是,UIButton而且btnAction没有框架,你还没有分配它,你正在执行CGRect popoverFrame = btnAction.frame;

它应该像

[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 300.0, 50.0) inView:self.view
               permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

希望这可以帮助。

于 2013-08-16T12:40:58.937 回答
1

你错过senderbtnAction

那是 UIButton *btnAction=(UIButton *)sender;

尝试这个,

- (IBAction)buttonTapped:(id)sender {

UIButton *btnAction=(UIButton *)sender;


UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *bookmarkVC = [storyboard instantiateViewControllerWithIdentifier:@"BookmarkViewController"];

_buttonPopoverController = [[UIPopoverController alloc]
                                initWithContentViewController:bookmarkVC];

_buttonPopoverController.delegate = self;

CGRect popoverFrame = btnAction.frame;
[bookmarkVC setContentSizeForViewInPopover:CGSizeMake(320, 355)];

//only required if using delegate methods

[_buttonPopoverController presentPopoverFromRect:popoverFrame
                                              inView:self.view
                            permittedArrowDirections:UIPopoverArrowDirectionUp
                                            animated:YES];
}
于 2013-08-16T12:48:25.647 回答