我第一次尝试使用NSInvocation,下面的代码是从stackoverflow的其他答案代码中采用的。
计时器运行正常,但它实际到期时崩溃并在(animationEnd:)
UIImageView* animationView = [animationViewArray objectAtIndex: i];
[self.imageView addSubview: animationView];
[animationView startAnimating];
// [NSTimer scheduledTimerWithTimeInterval: 5.5 target: self selector: @selector(animationEnd:) userInfo: animationView repeats: NO];
SEL selector = @selector(animationEnd:);
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
//The invocation object must retain its arguments
// when passing to timer, it's ok
// [animationView retain];
//Set the arguments
[invocation setTarget:self];
[invocation setArgument:&animationView atIndex:2];
[NSTimer scheduledTimerWithTimeInterval:0.1 invocation:invocation repeats:NO];
-(void) animationEnd:(NSInvocation*) invocation
{
UIImageView* animationView = nil;
[invocation getArgument:&animationView atIndex:2];
[animationView removeFromSuperview];
[animationView release];
}
我在哪里搞砸了? 基于崩溃日志,看起来像(animationEnd :)的调用是我传递给调用的参数本身 混乱的stuf ..
谢谢。
答案 0 :(得分:1)
不要发布animationView。你永远不会保留它。基本上,根据这段代码,我们看到三个人可能拥有它:调用(当它消失时谁将放弃所有权),名为animationViewArray
的数组(当视图从中移除时将放弃所有权),和animationView的超级视图(当你调用removeFromSuperview
时立即放弃对它的所有权。)
由于你不是这些,你不应该发布它。