2010-05-21 83 views
0

我想在touchesBegan方法中创建一个名为theImageView的UIImageView,然后我可以移动到touchesMoved中的新位置。目前,我在touchesMoved中收到“未声明”的错误,其中我设置了theImageView的新位置。在内存中保留对象(iPhone SDK)

我该怎么做才能保持theImageView这两种方法之间的内存?

编辑:我无法申报theImageView在@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); 
    ... 
} 

回答

6

你宣布一个方法/函数变量,这使得它的局部变量(即只存在内部的功能的变量)。要使其在其他方法中可用,必须在类的@interface中将其声明为实例变量。

+0

谢谢你的回答。我很抱歉,但我的意思是说,我无法这样做,因为我试图创建一个新的UIImageView对象每次触摸屏幕的预定区域。这意味着我需要动态创建这些对象,因此我无法在@interface中声明它们。有没有其他的方式来解决这个问题?再次感谢你的帮助。 – Chris 2010-05-21 05:38:07

+0

@Chris:我不明白为什么你认为你无法做到这一点。您可以重新分配实例变量以指向新的对象。 – Chuck 2010-05-21 05:49:36

+0

或者有一个实例变量来存放放置UIImageViews的NSMutableArray。 – 2010-05-21 06:38:35