Objective C:删除以前的 ui 元素
我每秒都在生产一块“砖头”。当你点击它时,它就会消失。我的问题是,当屏幕上出现第二个或更多时,点击前一个会消除最近的一个,并且根本无法从屏幕上删除。我的代码是:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您添加
sender
参数,它将自动设置为发送操作的控件,因此您可以执行如下操作: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:创建方法
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.
视图具有
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.