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

时间:2013-10-02 02:28:45

标签: ios uiactionsheet cgcontext

我正在使用操作表来显示供用户选择的数据列表。问题是使用[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出现问题:

after before

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

3 个答案:

答案 0 :(得分:3)

我认为这里的答案是UIActionSheet不打算以这种方式使用,因此可能会产生类似这样的副作用。从Apple ActionSheet documentation开始,“UIActionSheet不是设计为子类,也不应该为其层次结构添加视图。”

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

替代解决方案:

  1. 创建自己的视图并以模态方式呈现 - 我做了一个简单的example project,显示了一种方法。
  2. https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets(尚未更新iOS 7)

答案 1 :(得分:2)

Kyle,使用@“”之后你还有什么UI问题?

使用以下代码后,对我来说效果很好。我不使用tableview,我使用pickerview作为子视图。

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

答案 2 :(得分:0)

以下是我给出的类似question的答案

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

然而,这会弄乱您的操作表顶部的图形/绘图,所以如果您还没有背景,您只需添加此代码即可为您提供默认的操作表外观。

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

然后将其他任何内容添加到自定义操作表中。