Objective C:删除以前的 ui 元素

发布于 2024-10-09 01:16:36 字数 1016 浏览 1 评论 0原文

我每秒都在生产一块“砖头”。当你点击它时,它就会消失。我的问题是,当屏幕上出现第二个或更多时,点击前一个会消除最近的一个,并且根本无法从屏幕上删除。我的代码是:

- (NSTimer *)getTimer{  
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:     @selector(produceBricks) userInfo:nil repeats:YES];
return timer;
}


-(IBAction) tapBrick {
//remove last brick
[bricks[count] removeFromSuperview];

//add to score
count++;
NSString *scoreString = [NSString stringWithFormat:@"%d", count];
score.text = scoreString;
}

-(void) produceBricks {
//determine x y coordinates
int xPos, yPos;
xPos = arc4random() % 250;
yPos = arc4random() % 370;

//create brick
bricks[count] = [[UIButton alloc] initWithFrame:CGRectMake(xPos,yPos + 60,70,30)];  
[bricks[count] setBackgroundColor:[UIColor blackColor]];
[bricks[count] addTarget:self action:@selector(tapBrick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bricks[count]];


}

我知道这与bricks[count]removeFromSuperview行有关,计数总是在增加。我如何引用数组中正在单击的砖块而不是当前的砖块?

I'm producing a "brick" every second. When you tap it, it goes away. My problem is, when a second, or more, appear on the screen, tapping the previous one eliminates the most recent one, and can't be removed from the screen at all. The code I have is:

- (NSTimer *)getTimer{  
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:     @selector(produceBricks) userInfo:nil repeats:YES];
return timer;
}


-(IBAction) tapBrick {
//remove last brick
[bricks[count] removeFromSuperview];

//add to score
count++;
NSString *scoreString = [NSString stringWithFormat:@"%d", count];
score.text = scoreString;
}

-(void) produceBricks {
//determine x y coordinates
int xPos, yPos;
xPos = arc4random() % 250;
yPos = arc4random() % 370;

//create brick
bricks[count] = [[UIButton alloc] initWithFrame:CGRectMake(xPos,yPos + 60,70,30)];  
[bricks[count] setBackgroundColor:[UIColor blackColor]];
[bricks[count] addTarget:self action:@selector(tapBrick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bricks[count]];


}

I know it has to do with the bricks[count] removeFromSuperview line being count is always increasing. How would I reference to the brick in the array that's being clicked instead of the current one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网名女生简单气质 2024-10-16 01:16:36

如果您添加 sender 参数,它将自动设置为发送操作的控件,因此您可以执行如下操作:

-(IBAction) tapBrick:(id)sender {
    [sender removeFromSuperview];
}

If you add a sender argument, it'll automatically get set to the control which sent the action, so you can do something like the following:

-(IBAction) tapBrick:(id)sender {
    [sender removeFromSuperview];
}
你与清晨阳光 2024-10-16 01:16:36

创建方法tapBrick:(UIButton *)brick,您将获得对被点击的砖块的引用作为参数。

另外,我强烈建议不要为此使用 C 数组。它只是带来了内存管理的麻烦。最好使用 NSMutableArray< /a> 并确保遵循内存管理规则。使用您发布的代码,您的砖块对象将永远不会被释放,相反,它们只会继续消耗内存,直到您的应用程序耗尽其供应。

Make the method tapBrick:(UIButton *)brick and you'll get a reference to the brick that was tapped as the argument.

Also, I strongly advise against using a C array for this. It's just asking for memory management trouble. Better to use an NSMutableArray and make sure to follow the memory management rules. With the code you've posted, your brick objects will never be released, and instead they'll just keep eating memory until your app exhausts its supply.

转身以后 2024-10-16 01:16:36

视图具有 tag 属性是有原因的 - 因此您可以在同一视图控制器中识别相同类型的视图。考虑为每一块积木设置一个唯一的标签,然后删除您想要删除的特定标签。

Views have a tag property for a reason — So you can identify views of the same type in the same view controller. Consider setting a unique tag for each of these bricks, and then removing the particular one you want to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文