显示弹出窗口时,应用程序在更改方向时崩溃。

时间:2012-08-23 08:59:17

标签: objective-c ipad

朋友们,

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

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

}

谢谢..

1 个答案:

答案 0 :(得分:0)

在这里,您需要添加代码以在更改方向时从视图中关闭弹出窗口。

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

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

return YES;

}

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