ios actionsheet showinview导致崩溃解决方法

如果在代码中调用加入actionsheet的功能

1 UIActionSheet *actionSheet = [[UIActionSheet alloc]
2                                   initWithTitle:nil
3                                   delegate:self
4                                   cancelButtonTitle:ASButtonTitleCancel
5                                   destructiveButtonTitle:nil
6                                   otherButtonTitles:ASButtonTitleCamera, ASButtonTitleLibrary,nil];
7     actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
8     [actionSheet showInView:controller.view];

正常是可以调出来

这个选择菜单的

如果controller.view还没有添加到window的时候,系统就会报错崩溃。

报错内容如下:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Sheet can not be presented because the view is not in a window: <UIView: 0xce7df00; frame = (0 0; 320 480); autoresize = RM+BM; layer = <CALayer: 0xce51bb0>>'

******

这是因为在controller的viewdidload加入了actionview showinview的方法,但这个时候其实view还没有加入到window,不符合actionsheet的要求。

网上有其他一些解决方法,说加入到window中,或者检查下window的subviews是否已经包含当前view

我不是很建议使用上述方法,没有解决我的问题。

后来改在viewdidappear上去调用actionview showinview,这个时候view已经加入到window上了,就没有上述问题。

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     // Do any additional setup after loading the view, typically from a nib.
 4     
 5     //原先在这里添加actionsheet并showinview:self.view
 6     
 7 }
 8 
 9 - (void)viewDidAppear:(BOOL)animated {
10     
11   //修改后在这里添加actionsheet并showinview:self.view
12 }

参考的资料:

http://blog.csdn.net/hwak_07/article/details/27107319

http://stackoverflow.com/questions/18932544/nsinvalidargumentexception-reason-sheet-can-not-be-presented-because-the-vi 

原文地址:https://www.cnblogs.com/huangzizhu/p/4087573.html