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 :)での呼び出しは、呼び出しに渡した引数自体のように見えます。
紛らわしいstuf..

ありがとうございました。

4

1 に答える 1

1

AnimationViewを解放しないでください。あなたはそれを決して保持しませんでした。基本的に、このコードを考えると、それを所有している可能性のある3人の人がいます:呼び出し(ビューがなくなると所有権を放棄する)、名前の付いた配列animationViewArray(ビューが削除されたときに所有権を放棄する)、およびanimationViewのスーパービュー(電話をかけるとすぐに所有権を放棄する人removeFromSuperview)。

あなたはこれらのどれでもないので、あなたはそれを解放するべきではありません。

于 2010-12-29T10:32:52.417 に答える