当前位置 : 主页 > 手机开发 > ios >

ios – 从子视图控制器显示的UIActionSheet的iPad崩溃

来源:互联网 收集:自由互联 发布时间:2021-06-11
如果有人问过这个我道歉,但我似乎无法在任何地方找到它.我甚至在一个演示项目中重新创建了我的问题,以防你们想要亲眼看到它,尽管我不知道应该在哪里发布它. 我有一个基于xibles
如果有人问过这个我道歉,但我似乎无法在任何地方找到它.我甚至在一个演示项目中重新创建了我的问题,以防你们想要亲眼看到它,尽管我不知道应该在哪里发布它.

我有一个基于xibless UINavigationController的应用程序.我的一些子ViewControllers在顶部的右侧有一个按钮,然后显示一个UIActionSheet.我的应用程序专为iPhone和iPad设计,所以当我准备好显示UIActionSheet时,我会:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:@"%@ Menu", [self title]] delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Email", @"Print", nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleDefault];
if ([actionSheet respondsToSelector:@selector(showFromBarButtonItem:animated:)])
    [actionSheet showFromBarButtonItem:[[self navigationItem] rightBarButtonItem] animated:YES];
else [actionSheet showInView:[self view]];
[actionSheet release];

在iPad上,我试图将UIActionSheet显示在右侧条形按钮上,在iPhone上它应该从底部滑入.所有这一切都很美妙.

不幸的是,如果你点击按钮并在iPad上显示菜单,然后点击应用程序左上方的后退按钮,菜单就不会被忽略.相反,UINavigationController尽职尽责地回弹,UIActionSheet仍然存在.如果你尝试点击菜单上的东西,你当然会崩溃.如果用户在屏幕上点击了其他任何内容而不是“后退”按钮,则菜单会正确地解除.

如果您在iPhone上尝试此测试,一切都按预期工作.没有问题.

我的演示项目有一个AppDelegate和一个ViewController就是这样. AppDelegate构建了NSDictionaries的NSDictionary,因此我有一个模型,我可以通过它来演示这个问题. ViewController显示字典的所有键,如果相应的值是NSDictionary,您可以点击它以向下钻取.

这是一个有趣的问题.这就是 UIActionSheet Class Reference所说的.

On iPad, this method presents the action sheet in a popover and adds
the toolbar that owns the button to the popover’s list of passthrough
views. Thus, taps in the toolbar result in the action methods of the
corresponding toolbar items being called. If you want the popover to
be dismissed when a different toolbar item is tapped, you must
implement that behavior in your action handler methods.

因此,当您显示操作表时,它会自动创建UIPopoverController并将包含的工具栏(或导航栏)设置为popover的passthrough视图,从而允许触摸事件继续.我认为最好的办法是为你的工作表创建一个实例变量,并强制它在-viewWillDisappear中显示它是否可见:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    if (self.actionSheet.window) // If action sheet is on screen, window is non-nil
        [self.actionSheet dismissWithClickedButtonIndex:self.actionSheet.cancelButtonIndex animated:animated];
}
网友评论