首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么每一行代码都给了我“无效的上下文”?

为什么每一行代码都给了我“无效的上下文”?
EN

Stack Overflow用户
提问于 2012-04-02 02:53:58
回答 2查看 136关注 0票数 0

这是我的代码,当在iPad模拟器中运行应用程序时,其中的每一行都会给我一个“无效上下文”错误。"self.Frame = frame;“之后的每一条语句都会给出错误。我该如何修复它?

代码语言:javascript
运行
复制
- (void)makeFrame:(CGRect)frame number:(NSInteger) number color:(UIColor *) color {

    float rd1 = 225.00/255.00;
    float gr1 = 102.00/255.00;
    float bl1 = 0.00/255.00;

    float rd2 = 225.00/255.00;
    float gr2 = 153.00/255.00;
    float bl2 = 0.00/255.00;

    self.frame = frame;
    self.backgroundColor = [UIColor colorWithRed:rd1 green:gr1 blue:bl1 alpha:1.0]; 
    [[self layer] setBorderColor:[[UIColor blackColor] CGColor]]; 
    [[self layer] setBorderWidth:0.5];  
    [[self layer] setCornerRadius:10]; 
    self.tag = number;  //  set each button's tag
    [self setClipsToBounds: YES];

    //  do date label
    date = [[UILabel alloc]initWithFrame:CGRectMake(10, 2, 65, 40 )]; 
    date.backgroundColor = [UIColor clearColor]; //  x  y   w   h
    date.textColor = [UIColor colorWithRed:rd2 green:gr2 blue:bl2 alpha:1.0];
    date.text = [NSString stringWithFormat:@"%d", number];
    date.font = [UIFont boldSystemFontOfSize:40];
    //   date.alpha = 0.2;
    [self addSubview:date];
}

这是调用makeFrame的代码:

代码语言:javascript
运行
复制
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    allButtons = [NSMutableArray array];
    for(int k = 40, m = 0; m < 6; m++, k+= 42)   //  this controls the vertical distance between rows
        for(int i = 0, j=32; i < 7; i++, j+=102) {  //  this controls the size and horizontal distance
            calendarButton *cb= [calendarButton buttonWithType:UIButtonTypeCustom];
            [cb makeFrame:CGRectMake(j, k, 100, 40) number: allButtons.count+1 color:[UIColor orangeColor]];
            //                       x  y   w   h
            [self.view addSubview:cb];
            [allButtons addObject:cb];  // put it in the array
        }

    calendarButton *cb= [calendarButton buttonWithType:UIButtonTypeCustom];
    [cb drawRect:CGRectMake(0,0, 100, 40)];   //  150
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-02 03:12:48

您正在手动调用drawRect,我怀疑这是导致此问题的原因。从文档中:

您永远不应该自己直接调用此方法。要使视图的一部分无效,从而导致该部分被重新绘制,请改为调用setNeedsDisplaysetNeedsDisplayInRect:方法。

不过,在这种情况下,这看起来不是必要的。该视图已被添加为视图的子视图;当视图呈现时,它将自动绘制自身。

票数 1
EN

Stack Overflow用户

发布于 2012-04-02 03:09:16

您正在尝试访问视图控制器中的视图属性。在您的情况下,self为UIViewController (或sublcass),请将其更改为self.view以访问视图

例如。

代码语言:javascript
运行
复制
self.view.frame = frame;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9967252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档