Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'NSInvalidArgumentException', reason: 'Sheet can not be presented because the view is not in a window

After upgrading Xcode from Version 4 to 5 and therefore from iOS 6 to iOS 7 i get the following error:

'NSInvalidArgumentException', reason: 'Sheet can not be presented because the view is not in a window

in this line:

[actionSheet showInView:self.view];
like image 687
Thorsten Niehues Avatar asked Sep 21 '13 12:09

Thorsten Niehues


3 Answers

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]; } 
like image 174
Jeremy Fox Avatar answered Sep 19 '22 22:09

Jeremy Fox


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.

like image 32
Reinhard Männer Avatar answered Sep 20 '22 22:09

Reinhard Männer


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:

[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject]

for example:

[actionSheet showInView:[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject]];
like image 33
Kapil Chandel Avatar answered Sep 20 '22 22:09

Kapil Chandel