xcode 'NSInvalidArgumentException',原因:'由于视图不在窗口中,所以无法显示工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18932544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'NSInvalidArgumentException', reason: 'Sheet can not be presented because the view is not in a window
提问by Thorsten Niehues
After upgrading Xcode from Version 4 to 5 and therefore from iOS 6 to iOS 7 i get the following error:
将 Xcode 从版本 4 升级到 5 并因此从 iOS 6 升级到 iOS 7 后,我收到以下错误:
'NSInvalidArgumentException', reason: 'Sheet can not be presented because the view is not in a window
'NSInvalidArgumentException',原因:'由于视图不在窗口中,所以无法显示工作表
in this line:
在这一行:
[actionSheet showInView:self.view];
回答by Jeremy Fox
I literally just had this exact same problem and unfortunately I'm still not sure what the root cause of the problem is. However, you'll find my current solution below. If I make any progress on root causing the issue I'll let you know.
我确实遇到了这个完全相同的问题,不幸的是,我仍然不确定问题的根本原因是什么。但是,您会在下面找到我当前的解决方案。如果我在导致问题的根源上取得任何进展,我会通知您。
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
if ([window.subviews containsObject:self.view]) {
[emailSheet showInView:self.view];
} else {
[emailSheet showInView:window];
}
回答by Reinhard M?nner
I got the same problem. In my code, the reason obviously was that I wanted to show an action sheet in viewWillAppear:
.
After moving the code to viewDidAppear:
, the error was gone.
我遇到了同样的问题。在我的代码中,原因显然是我想在viewWillAppear:
. 将代码移至 后viewDidAppear:
,错误消失了。
回答by Kapil Chandel
I don't know what is the root cause of the problem is, but I found a solution which is working for me. In place of self.view
, place this line:
我不知道问题的根本原因是什么,但我找到了一个对我有用的解决方案。代替self.view
,放置以下行:
[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject]
for example:
例如:
[actionSheet showInView:[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject]];
回答by Refael.S
I had the same problem and in my case this happened when I tried to show an ActionSheet on my self.view while my self.view was presenting another viewController.
我遇到了同样的问题,在我的情况下,当我尝试在 self.view 上显示 ActionSheet 而我的 self.view 呈现另一个 viewController 时,就会发生这种情况。
This is the crash: "Sheet can not be presented because the view is not in a window"
这是崩溃:“由于视图不在窗口中,因此无法显示工作表”
example for the problem:
问题示例:
[modalViewController dismissModalViewControllerAnimated:YES];
[actionSheet showInView:self.view];
I solved the problem by waiting for the modalViewController to dismiss and then show the ActionSheet.
我通过等待 modalViewController 关闭然后显示 ActionSheet 来解决这个问题。
Solution:
解决方案:
[modalViewController dismissModalViewControllerAnimated:YES];
[actionSheet performSelector:@selector(showInView:) withObject:self.view afterDelay:0.6];
Hope this helps a lot of people :)
希望这可以帮助很多人:)
回答by omi5489
I had the same issue [actionSheet showInView:self.view];
and I solved it with [actionSheet showInView:[UIApplication sharedApplication].keyWindow];
.
我有同样的问题[actionSheet showInView:self.view];
,我用[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
.
Perhaps the problem is related to the paragraph of the Apple documentation:
也许问题与Apple文档的段落有关:
Subclassing Notes:
UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with
presentViewController:animated:completion:
.
子类化注意事项:
UIActionSheet 并非设计为子类化,也不应将视图添加到其层次结构中。如果您需要展示比 UIActionSheet API 提供的更多定制的工作表,您可以创建自己的工作表并使用
presentViewController:animated:completion:
.
回答by Palleraccio
I had a similar problem with a toolbar and it should be better always to check if the passed view to the selector showInView
has a window not nil. For example:
我在工具栏上遇到了类似的问题,最好总是检查传递给选择器的视图showInView
是否有一个不是 nil 的窗口。例如:
if (self.view.window)
[actionSheet showInView:self.view];
In this way, we can avoid annoying exceptions. Then, according to your application layout, you should search a valid view to show the action sheet. If your app has a tabbar or a toolbar, it should be better to use
这样,我们就可以避免恼人的异常。然后,根据您的应用程序布局,您应该搜索一个有效的视图以显示操作表。如果您的应用程序有标签栏或工具栏,最好使用
- (void)showFromToolbar:(UIToolbar *)view;
- (void)showFromTabBar:(UITabBar *)view;
回答by David Knight
I have previously used
我以前用过
- (void)showFromTabBar:(UITabBar *)view;
and hit a similar problem with one of my controllers (I have a utility method that handles showing action sheets from various controllers) as it sets
并在我的一个控制器上遇到了类似的问题(我有一个实用方法可以处理显示来自各种控制器的动作表),因为它设置
hidesBottomBarWhenPushed = YES;
With iOS 7 this now appears to set window to nil on the tabBar causing the error mentioned. My workaround is to check the window property of the tabBar and if nil call
在 iOS 7 中,现在似乎将 tabBar 上的 window 设置为 nil,从而导致上述错误。我的解决方法是检查 tabBar 的 window 属性,如果 nil 调用
if (nil == tabBar.window) {
[actionSheet showInView:self.window];
}
else {
[actionSheet showFromTabBar:tabBar];
}
回答by RickJansen
In my case this happens when a viewcontroller that has just been pushed onto the stack decides it wants to show an alert sheet. But, apparently the viewcontroller is not yet entirely ready to do that right after it is pushed. I use [actionSheet showInView:self.parentViewController.view];
instead of [actionSheet showInView:self.view];
.
在我的例子中,当一个刚刚被推到堆栈上的视图控制器决定它想要显示一个警报表时,就会发生这种情况。但是,显然视图控制器还没有完全准备好在推送后立即执行此操作。我使用[actionSheet showInView:self.parentViewController.view];
代替[actionSheet showInView:self.view];
.
回答by Udit Agarwal
In iOS 7, the view send to the showInView:
should be the part of the view hierarchy. It should be added as the subview of some view.
You can check whether some view can be used in showInView:
by checking whether the view.window
is nil or not.view.superview
should also be non-nil
在 iOS 7 中,发送到 的视图showInView:
应该是视图层次结构的一部分。它应该作为某些视图的子视图添加。
您可以showInView:
通过检查是否view.window
为 nil来检查是否可以使用某些视图。view.superview
也应该是非零