【问题标题】:Application crashes on changing orientation while popover is displayed.显示弹出框时更改方向时应用程序崩溃。
【发布时间】:2012-08-23 08:59:17
【问题描述】:

朋友们,

单击工具栏按钮时,会出现我的弹出视图。当我将模拟器旋转到横向时,会显示以下错误消息:

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

*** First throw call stack:
(0x1b69052 0x1868d0a 0x1b11a78 0x1b119e9 0x9ae72e 0x9affcb 0x9b26eb 0x281a39 0x1b34885 0x1b347a8 0x1c61aa 0x5490d8 0x54d499 0x54d584 0x11ede00 0x15ff4f0 0x1aa0833 0x1a9fdb4 0x1a9fccb 0x209a879 0x209a93e 0x51ca9b 0x29dd 0x2955)

处理我的按钮事件的代码是:

-(IBAction)name:(id)sender
{
    if ([_popover isPopoverVisible]) 
    {
        [_popover dismissPopoverAnimated:YES];
    }

    else {
        task * content = [[task  alloc] init];

        content.navigationItem.title =@"name"; 
        [content setDelegate:self];
        navController = [[UINavigationController alloc] initWithRootViewController: content]; 
        _popover = [[UIPopoverController alloc] 
                    initWithContentViewController:navController];
        _popover. popoverContentSize=CGSizeMake(350, 480);

        [_popover presentPopoverFromBarButtonItem:sender
                        permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }

}

谢谢..

【问题讨论】:

标签: objective-c ipad


【解决方案1】:

您需要在此处添加代码以在更改方向时从视图中消除弹出框。

if ([_popover isPopoverVisible]) { [_popover dismissPopoverAnimated:YES]; }

因此,在更改方向时将代码放入方法 shouldAutorotateToInterfaceOrientation() 中,它将起作用。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

if ([_popover isPopoverVisible]) 
{
    [_popover dismissPopoverAnimated:YES];
}

return YES;

}

如果您还有其他问题,请随时提出。

【讨论】:

  • 你能告诉我为什么我们必须在方向改变时关闭这个弹出窗口是的它工作但它没有错误的方法请解释我
  • 这是弹出框控制器的默认行为,正如您通过 BarButtonItem 添加的那样。所以,当你旋转它时,它会改变位置。但是在这里你仍然可以让它按照你的需要工作。在方向从当前方向更改为另一种模式后,只需调用相同的函数即可添加弹出框。因此,首先删除弹出框,同时根据您的方向再次添加,我认为您希望拥有该功能。如果再有任何问题,请告诉我,我会帮你解决这个问题。
  • 那么如何在横向模式下保持弹出视图的引用也是另一种方法我在方向代码上调用相同的函数但它不起作用请解释它是如何工作的
  • 这里在你改变你的方向之后,你可以调用该函数来显示弹出框。现在要跟踪您的方向,您可以使用一个存储为横向或纵向的变量,并在此基础上显示您的弹出框功能并设置帧大小等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多