fw2601
fw2601

Reputation: 83

Objects created in initWithFrame method not know in another method of the same class

This is the proof that I don't understand some fundamentals, but it's still the first week in OO! ;)

Within my view class this object is created in the initWithFrame-method:

- (id)initWithFrame:(NSRect)frame{
    self = [super initWithFrame:frame];
    if (self)
{
    SeqModel *seq1 = [[SeqModel alloc] init];
    [seq1 setSeqSteps:16];
    [seq1 setSeqPatterns:1];
    [seq1 setName:@"Load sample here"];
    }
    return self;
}

Later its called in another method:

- (void)drawSampleNameSeq1{
    NSLog(@"%@",[seq1 sampleName]);
    [self drawText:[seq1 sampleName] schrift:@"Helvetica Light" r:140 g:140 b:140 tsize:15     xpos:1005 ypos:755 ];
}

and then the compiler says: "Use of undeclared identifier 'seq1' ". Doing the same in the method that creates the object works...what to do? Thanks.

Upvotes: 0

Views: 313

Answers (1)

Lithu T.V
Lithu T.V

Reputation: 20021

The problem is with the variable scope.Here the variable you declared has scope or life inside the braces alone,hence it is not available for the other methods.

Solution

Create an instance variable : a variable which has access everywhere within the class.

in your .h

@interface ClassName : SuperClass {
   SeqModel *seq1;
}

Then in the initWithFrame: Method

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        seq1 = [[SeqModel alloc] init];
        [seq1 setSeqSteps:16];
        [seq1 setSeqPatterns:1];
        [seq1 setName:@"Load sample here"];
    }
    return self;
}

Upvotes: 1

Related Questions