您的位置:首页 > 移动开发 > IOS开发

ios actionsheet showinview导致崩溃解决方法

2014-11-10 16:56 423 查看
如果在代码中调用加入actionsheet的功能

UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:ASButtonTitleCancel
destructiveButtonTitle:nil
otherButtonTitles:ASButtonTitleCamera, ASButtonTitleLibrary,nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[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上了,就没有上述问题。

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//原先在这里添加actionsheet并showinview:self.view

}

- (void)viewDidAppear:(BOOL)animated {

//修改后在这里添加actionsheet并showinview:self.view
}


参考的资料:

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

http://stackoverflow.com/questions/18932544/nsinvalidargumentexception-reason-sheet-can-not-be-presented-because-the-vi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐