【问题标题】:Showing actionsheet causes CGContext invalid context errors显示操作表会导致 CGContext 无效上下文错误
【发布时间】:2013-10-08 08:59:32
【问题描述】:

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

【问题讨论】:

    标签: ios uiactionsheet cgcontext


    【解决方案1】:

    这是我在类似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];
    

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

    【讨论】:

    • @BraveS 到底发生了什么?
    • ios7中背景还是透明的
    【解决方案2】:

    我相信这里的答案是 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 还没有更新)

    【讨论】:

    • 确实,我们试图将操作表用作穷人的 iPhone 弹出框。感谢@kyle 的回答,我们做了一些重构,所以现在我们只需在模态 UINavigationController 中呈现内容。
    【解决方案3】:

    Kyle,您在使用@"" 后还有什么其他 UI 问题?

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

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

    【讨论】:

      猜你喜欢
      • 2013-10-30
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      相关资源
      最近更新 更多