为什么我的变量保留在drawRect中?

时间:2015-07-01 19:44:33

标签: objective-c macos

我试图让nsview将自定义对象作为参数,然后根据该对象绘制一些文本字段。

-(instancetype)initWithFrame:(NSRect)frameRect parent:(id)parent item:(TempObj*)item{
    self = [super initWithFrame:frameRect];
    if (self){
        textDelegate = parent;
        myObject = item;
    }
    return self;
}

但到达drawRect时,myObject变量为nil。

- (void)drawRect:(NSRect)dirtyRect {
    if (myObject != nil){
        NSTextField* title = [[NSTextField alloc]initWithFrame:CGRectMake(textRoot.x, textRoot.y-20, 200, 20)];
        title.stringValue = [myObject getName];
        title.backgroundColor = [NSColor yellowColor];
        [self addSubview:title];
        NSLog(@"here");

    }else{
        NSLog(@"broken");
    }
}

视图在另一个类中初始化:

 _inventoryItemView = [[InventoryItemView alloc]initWithFrame:_inventoryItemView.frame parent:self item:testObj];

我是否有某种方法可以维护变量,或者是否存在我缺少的范围?

0 个答案:

没有答案