我有一個UIView子類,在此我調用這個其他UIView子類。爲什麼在預期使用ARC時不調用類dealloc?
Stars.h
@interface Stars : UIView {
BOOL _gained;
}
@property (nonatomic, weak) id<NSObject> delegate;
@property (nonatomic) BOOL gained;
-(void)animateAndRemove;
@end
Stars.m
#import "Stars.h"
@implementation Stars
@synthesize delegate = _delegate;
@synthesize gained = _gained;
- (id)initWithFrame:(CGRect)frame
{
frame.size.width = 31;
frame.size.height = 30;
self = [super initWithFrame:frame];
if (self) {
_gained = NO;
self.backgroundColor = [UIColor clearColor];
// Add star
UIImageView *star = [[UIImageView alloc] initWithFrame:frame];
[star setImage:[UIImage imageNamed:@"star-sticker"]];
[self addSubview:star];
}
return self;
}
- (void)animateAndRemove
{
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
self.transform = transform;
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
self.transform = transform;
CGAffineTransform move = CGAffineTransformMakeTranslation(0, -200);
self.transform = move;
self.alpha = 0.0;
}
completion:^(BOOL finished){
if ([self.delegate respondsToSelector:@selector(removeStar:)]) {
[self.delegate performSelector:@selector(removeStar:) withObject:self];
}
}];
}];
}
- (void)dealloc
{
NSLog(@"%s",__FUNCTION__);
}
@end
這只是增加了一個圖像,這是我除去之前動畫。像這樣的動畫
star1 = [[Stars alloc] init];
star1.center = self.center;
star1.delegate = self;
[self addSubview:star1];
,並開始刪除過程: 我這個添加到原來的UIView這樣
if (CGRectIntersectsRect(thumbR, star1.frame)) {
if (!star1.gained) {
star1.gained = YES;
[star1 animateAndRemove];
}
}
,當完成調用此:
- (void)removeStar:(Stars *)star
{
star.delegate = nil;
[star removeFromSuperview];
star = nil;
}
現在當類從removeFromSuperview中設置爲nil時,dealloc中的NSLog不會被調用。爲什麼不?
我實際上看到了第一個加載這個Stars類的第一個UIView。當我認爲它應該的時候,那個也不會被釋放。 當我通過alloc重新分配包含的UIView並再次啓動它時,這兩個dealloc方法都是完整的。
我錯了,期待這個dealloc當它從視圖中刪除,並設置爲零?
您正在傳遞一個引用值,而不是引用引用,這就是爲什麼。 –
你能詳細說一下嗎?我不太明白你的意思。謝謝 – Darren