我是第一次尝试使用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..
谢谢。
发布于 2010-12-29 18:32:52
不要释放animationView。你从来没有保留过它。基本上,给定这段代码,我们看到可能拥有它的三个人:调用(当它消失时将放弃所有权),名为animationViewArray的数组(当视图从其中移除时将放弃所有权),以及动画视图的superview (当您调用removeFromSuperview时立即放弃所有权)。
既然你不是这些,你就不应该发布它。
https://stackoverflow.com/questions/4553270
复制相似问题