我创建了一组自定义类型的对象。所有对象的方法showDeleteButton
具有hideDeleteButton
。
我发现当我隐藏删除按钮(删除它)时,按下的按钮具有retainCounter == 2。
这里的代码:
-(void)showDeleteButton {
if(!isDeleteButtonLoaded) { // Check that method was't triggered twice
UIButton *aDeleteButton = [[UIButton alloc] initWithFrame:CGRectMake(-3, -7, 30, 29)]; // RC == 1
[aDeleteButton setImage:[UIImage imageNamed:@"close_button.png"] forState:UIControlStateNormal];
[self addSubview:aDeleteButton]; // RC == 2
[aDeleteButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
deleteButton = aDeleteButton;
[aDeleteButton release]; // RC == 1
isDeleteButtonLoaded = YES;
NSLog(@"delete button retain count (created): %d", [deleteButton retainCount]);
[self setNeedsDisplay];
}
}
-(void)deleteButtonPressed:(id)sender {
[delegate deleteImageAtPath:self.imageFullPath];
}
-(void)hideDeleteButton {
if(isDeleteButtonLoaded) {
NSLog(@"delete button retain count (before): %d", [deleteButton retainCount]); // RC == 1
[deleteButton removeFromSuperview]; // RC == 0
deleteButton = nil;
isDeleteButtonLoaded = NO;
[self setNeedsDisplay];
}
}
因此,在第二种方法中,对于按下的按钮NSLog,显示RC == 2!有任何想法吗?我100%肯定不能从类外部访问deleteButton。
最佳答案
请勿留存
hem
对于任何理智的人而言,retainCount毫无意义,只要您遵循内存规则,一切都会正常进行。
关于objective-c - UIButton触摸后保持计数器增加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6347394/