显示操作表导致CGContext无效的上下文错误

我正在使用操作表来显示供用户select的数据列表。 问题是显示使用[self.actionSheet showInView:self.view]; 导致几个CGContext错误。 相同的代码在iOS 6中运行良好。

码:

 self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; [self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque]; CGRect tableFrame = CGRectMake(0, 40, 320, 214); self.tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain]; self.tableView.dataSource = self; self.tableView.delegate = self; [self.actionSheet addSubview:self.tableView]; UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]]; closeButton.momentary = YES; closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f); closeButton.tintColor = [UIColor redColor]; [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged]; [self.actionSheet addSubview:closeButton]; [self.actionSheet showFromView:self.view]; [UIView beginAnimations:nil context:nil]; [self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)]; [UIView commitAnimations]; 

错误:

 CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. CGContextSetStrokeColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. CGContextSetFlatness: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. CGContextDrawPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. 

更新:

将cancelButtonTitle设置为@“”的解决方法会导致以下UI问题:

后之前

原始代码来自另一个stackoverflow的答案,请参阅https://stackoverflow.com/a/2074451/654870 。

我相信这里的答案是,UIActionSheet不打算用这种方式,因此可能有这样的副作用。 从Apple ActionSheet文档中 ,“UIActionSheet不是被devise为子类,也不应该将视图添加到其层次结构中”。

虽然这在iOS 6中并没有给我带来任何问题,但iOS 7中的一些内容已经发生了明显的变化,我更倾向于采用另一种方式,而不是尝试去做与文档相矛盾的事情。 请注意,解决方法可能是传递@“”为cancelButtonTitle,而不是零,但这会导致其他UI问题,可能不会被Apple批准。

替代scheme:

  1. 创build您自己的视图并以模态方式呈现 – 我做了一个简单的示例项目,展示了一种方法来实现此目的。
  2. https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets(iOS 7尚未更新)

凯尔,使用@“”后还有什么其他的用户界面问题?

我用下面的代码之后,对我来说工作正常。 我不使用tableview,但我使用pickerview作为子视图。

  self.startSheet=[[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:@"" destructiveButtonTitle:nil otherButtonTitles:nil]; 

这是我在类似的问题上给出的答案,效果很好

 self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 

但是,如果你没有背景,就会把graphics/绘图搞乱,所以如果你没有背景,只需要添加这个代码就可以给你默认的动作表。

 UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; background.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1]; [self.actionSheet addSubview:background]; 

然后添加任何你想要的自定义操作表。