2

我试图在 UIBarButtonItem 上显示一个 UIPopover。我收到此错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromBarButtonItem:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'

这是我的代码:

Pop *controller;
UIPopoverController *popoverController;
controller = [[Pop alloc] initWithNibName:@"Pop" bundle:nil];
popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];

[popoverController presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

解决方案

现在效果很好:

Pop *controller = [[Pop alloc] initWithNibName:@"Pop" bundle:nil];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
popoverController.delegate=self;

[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
4

1 回答 1

1

弹出框必须呈现在另一个视图控制器的顶部,您不能让弹出框漂浮在半空中。只需创建一个视图控制器以在其上呈现视图控制器。

UIViewController *vc = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];

然后创建并展示您的弹出框。

于 2013-08-31T20:28:05.190 回答