gpt4 book ai didi

objective-c - 设置为 nil 的界面元素仍然存在于应用程序中

转载 作者:行者123 更新时间:2023-11-28 18:24:58 25 4
gpt4 key购买 nike

在我的游戏中,我使用以下代码以编程方式分配了一些 UIImageView:

blocks[1] = [[UIImageView alloc] initWithFrame: CGRectMake(200, y1, kBlockHeight, kBlockHeight)];
blocks[2] = [[UIImageView alloc] initWithFrame: CGRectMake(320, y2, kBlockHeight, kBlockHeight)];
blocks[3] = [[UIImageView alloc] initWithFrame: CGRectMake(440, y3, kBlockHeight, kBlockHeight)];
blocks[4] = [[UIImageView alloc] initWithFrame: CGRectMake(560, y4, kBlockHeight, kBlockHeight)];

blocks[1].backgroundColor = [UIColor blackColor];
blocks[2].backgroundColor = [UIColor blackColor];
blocks[3].backgroundColor = [UIColor blackColor];
blocks[4].backgroundColor = [UIColor blackColor];

[self.view addSubview: blocks[1]];
[self.view addSubview: blocks[2]];
[self.view addSubview: blocks[3]];
[self.view addSubview: blocks[4]];

它工作正常,但是当我尝试摆脱对象并重新启动时,

for (int i = 1; i<= 4; i++) {
blocks[i] = nil;
//ARC won't let me call [blocks[i] release];
NSLog(@"releasing blocks");
}

它们仍然停留在屏幕上。它们不与任何东西交互,但仍停留在屏幕上。这是为什么,我应该怎么做才能摆脱这些对象?

最佳答案

您的 UIImageViews 仍由您的 viewController 的 View 保留。尝试使用:

for (int i = 1; i<= 4; i++) {
[blocks[i] removeFromSuperview];
blocks[i] = nil;
}

关于objective-c - 设置为 nil 的界面元素仍然存在于应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12075505/

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