将对象保留在内存中(iPhone SDK)

0 memory iphone cocoa objective-c uiimageview

我试图在touchesBegan方法中创建一个名为theImageView的UIImageView ,然后我可以移动到touchesMoved中的新位置.目前我在touchesMoved中收到"未声明"错误,我在其中设置了ImageView的新位置.

我该怎么做才能在这两种方法之间将ImageView保存在内存中?

编辑:我无法在@interface中声明图像视图,因为我需要在每次触摸某个点时创建图像.

- (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);
    ...
}
Run Code Online (Sandbox Code Playgroud)

Cos*_*que 6

您已在方法/函数中声明了该变量,这使得它成为局部变量(即,仅存在于该函数内的变量).要使其在其他方法中可用,您必须在类的@interface中将其声明为实例变量.