gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:29:48 25 4
gpt4 key购买 nike

我正在尝试在touchesBegan方法中创建一个名为theImageView的UIImageView,然后我可以将其移动到touchesMoved中的新位置。目前,我在 TouchMoved 中收到“未声明”错误,其中我为 theImageView 设置了新位置。

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

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

- (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);
...
}

最佳答案

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

关于iphone - 将对象保存在内存中(iPhone SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2879675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com