弹出窗口不能从没有窗口的视图中呈现 - presentPopoverFromBarButtonItem

时间:2013-08-31 20:20:28

标签: ios ipad uipopovercontroller uibarbuttonitem

我试图在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];

1 个答案:

答案 0 :(得分:1)

必须在另一个视图控制器的顶部显示弹出窗口,你不能在半空中浮动弹出窗口。只需创建一个视图控制器,即可将视图控制器呈现在。

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

然后创建并展示你的popover。