Chris
Chris

Reputation: 11

Keeping object in memory (iPhone SDK)

I am trying to create a UIImageView called theImageView in the touchesBegan method that I can then then move to a new location in touchesMoved. Currently I am receiving an "undeclared" error in touchesMoved where I set the new location for theImageView.

What can I do to keep theImageView in memory between these two methods?

EDIT: I am unable to declare theImageView in the @interface as I need to create an image every time a certain point is touched.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    ...
    UIImageView *theImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
    theImageView.frame = CGRectMake(263, 228, 193, 300);
    [theImageView retain];
    ...
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    ...
    theImageView.frame = CGRectMake(300, 300, 193, 300);
    ...
}

Upvotes: 0

Views: 260

Answers (1)

Costique
Costique

Reputation: 23722

You have declared the variable in a method/function, which makes it a local variable (that is, a variable which exists only inside that function). To make it available in other methods you have to declare it as instance variable in your class's @interface.

Upvotes: 6

Related Questions