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

10,166

Solution 1

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];
}

Solution 2

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.

Solution 3

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]];

Solution 4

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.

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.

Solution:

[modalViewController dismissModalViewControllerAnimated:YES];
[actionSheet performSelector:@selector(showInView:) withObject:self.view afterDelay:0.6];

Hope this helps a lot of people :)

Solution 5

I had the same issue [actionSheet showInView:self.view]; and I solved it with [actionSheet showInView:[UIApplication sharedApplication].keyWindow];.

Perhaps the problem is related to the paragraph of the Apple documentation:

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:.

Share:
10,166
Thorsten Niehues
Author by

Thorsten Niehues

Working for Sybit & Peers, specializing in SAP HANA Worked as Head of IT for ContiTech North America Worked for many other companies (e.g. Bosch, T-Systems, IBM) programming in many languages (Java, Objective-C, PHP, JavaScript, etc.) and worked a lot with many databases (DB2, Oracle, MySQL, etc.) Studied computer science (Master) at HTWG Konstanz Studied computer science (Bachelor) at HFT Stuttgart Ongoing project: FindClimb an app which helps climbers to find their way to a climbing spot Technologies: iOS, Android, Google-Maps, Google-Shopping, HTML, Objective-C, Java, JS, VB, VBA, Perl, Shell, XML, XSD, Xcode, Eclipse, Linux (Ubuntu/Solaris/Suse/RedHat), OS X (10.8 Mountain Lion / 10.10 Yosemite), Windows (XP/Windows Server 2008/Vista/Windows7/Windows8/Windows10)

Updated on June 23, 2022

Comments

  • Thorsten Niehues
    Thorsten Niehues almost 2 years

    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];