iPad 崩溃并从子视图控制器显示 UIActionSheet
如果有人问这个问题,我深表歉意,但我似乎无法在任何地方找到它。我什至在演示项目中重新创建了我的问题,以防你们中的任何人想亲眼看到它,尽管我不知道应该将其发布在哪里。
我有一个基于 xibless UINavigationController 的应用程序。我的一些子 ViewController 在顶部右侧有一个按钮,然后显示 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 构建了一个 NSDictionary 的 NSDictionary,这样我就有了一个可以递归地演示问题的模型。 ViewController 显示了字典的所有键,如果对应的值是 NSDictionary,您可以点击它进行向下钻取。
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.
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];
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.
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.
If you try this test on iPhone, everything works as expected. There is no issue.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个有趣的问题。这是 UIActionSheet 类参考 不得不说。
因此,当您显示操作表时,它会自动创建 UIPopoverController 并将包含的工具栏(或导航栏)设置为弹出窗口的直通视图,从而允许触摸事件继续。我认为最好的选择是为您的操作表创建一个实例变量,并强制它在
-viewWillDisappear:
中可见时关闭。This is an interesting problem. Here's what the UIActionSheet Class Reference has to say.
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:
.您是否尝试过强制关闭 viewWillDisappear 上的 ActionSheet?
试试这个:
*崩溃听起来可能是 EXC_BAD_ACCESS。当您因发布而更改视图时,您可能会丢失对“actionSheet”的指针引用。在 .h 文件中保留对 actionSheet 的引用并管理发布的时间可能会很好。
*另请参阅文档以获取有关关闭消息的信息: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html
Have you tried force-dismissing the ActionSheet on viewWillDisappear?
Try this:
*The crash sounds like a possible EXC_BAD_ACCESS. You might be losing your pointer reference to 'actionSheet' when you change views due to your release. Might be good to hang on to a reference to actionSheet in your .h file and manage the timing of your release.
*Also see the docs for info about the dismiss message: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html