0

我第一次尝试使用 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:) 处的调用是我传递给调用的参数本身。
混乱的东西。。

谢谢你。

4

1 回答 1

1

不要释放动画视图。你从来没有保留它。基本上,给定这段代码,我们看到三个可能拥有它的人:调用(当它消失时谁将放弃所有权)、命名数组animationViewArray(当视图从其中删除时谁将放弃所有权)和动画视图的超级视图(谁在您致电时立即放弃所有权removeFromSuperview)。

因为你不是这些,你不应该释放它。

于 2010-12-29T10:32:52.417 回答