iPad崩溃与UIActionSheet从子视图控制器显示 [英] iPad crash with UIActionSheet displayed from child view controller

查看:273
本文介绍了iPad崩溃与UIActionSheet从子视图控制器显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这已被问过,我道歉,但我似乎无法在任何地方找到它。我甚至在一个演示项目中重新创建我的问题,以防任何人想要亲眼看到它,虽然我不知道应该在哪里发布。

I apologize if this has been asked but I can't seem to find it anywhere. I even recreated my issue in a demo project in case any of you want to see it first-hand, although I don't know where I should post it.

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

I have a xibless UINavigationController based app. Some of my child ViewControllers have a button on the right side at the top that then displays a UIActionSheet. My app is designed for iPhone and iPad, so when I get ready to display the UIActionSheet I do:

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应从底部滑入。

On iPad, I'm trying to show the UIActionSheet attached to the right bar button and on iPhone it should slide in from the bottom. All of this works beautifully.

不幸的是,如果你点击按钮,并在iPad上显示菜单,但点击应用程序左上角的后退按钮,菜单不会关闭。相反,UINavigationController充分地弹出,UIActionSheet仍然存在。如果你试图点击菜单上的东西,你当然会崩溃。如果用户已经点击屏幕上的任何其他东西,而不是后退按钮,菜单正确地关闭。

Unfortunately, if you tap the button and show the menu on iPad, but then tap the back button on the top left side of the app, the menu doesn't dismiss. Instead UINavigationController dutifully pops back and the UIActionSheet is still there. If you try to tap something on the menu you of course get a crash. If the user would have tapped anything else on the screen instead of the Back button, the menu properly dismisses.

如果你尝试这个测试在iPhone上,一切正常工作。没有问题。

If you try this test on iPhone, everything works as expected. There is no issue.

我的演示项目有一个AppDelegate和一个ViewController。 AppDelegate构建NSDictionary的NSDictionaries只是所以我有一个模型,我可以递归来演示这个问题。 ViewController显示字典的所有键,如果相应的值是NSDictionary,您可以点击它来向下钻取。

My demo project has an AppDelegate and a ViewController and that's about it. The AppDelegate builds an NSDictionary of NSDictionaries just so I have a model I can recurse through to demonstrate the issue. The ViewController shows all of the keys of the dictionary and if the corresponding value is an NSDictionary, you can tap it to drill down.

推荐答案

p>这是一个有趣的问题。以下是

This is an interesting problem. Here's what the UIActionSheet Class Reference has to say.


在iPad上,此方法在弹出窗口中显示操作表,并添加
拥有按钮的工具栏到popover的passthrough
视图列表。因此,工具栏中的敲击会导致
对应的工具栏项的操作方法被调用。如果你想要弹出到
,当点击不同的工具栏项时,你必须
在你的动作处理程序方法中实现这个行为。

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:中可见时关闭。

So when you display the action sheet, it's automatically creating a UIPopoverController and set the containing toolbar (or navigation bar) as the popover's passthrough views, allowing touch events to continue. I think the best bet is to create an instance variable for your action sheet and to force it to dismiss if it is visible in -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];
}

这篇关于iPad崩溃与UIActionSheet从子视图控制器显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆