0

プロジェクトで作業しているときに、別のクラスでインスタンス化するクラスを作成しました(特別なことは何もありません)が、このインスタンスのメソッドを呼び出そうとすると、次のようになります。

キャッチされなかった例外'NSInvalidArgumentException'が原因でアプリを終了しています、理由:'-[CALayer animate]:認識されないセレクターがインスタンスに送信されました

これがクラス.hです:

#import <QuartzCore/QuartzCore.h>

@interface SFStripe : CALayer {

        NSTimer *animation;

}

- (id)initWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY;
- (id)initEndedWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY;
- (void)animate;
- (void)reduceSize;
- (void)logSomething;

@property NSTimer *animation;


@end

そしてここで.m:

#import "SFStripe.h"

@implementation SFStripe

@synthesize animation;

- (id)initWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY {

    self = [CALayer layer];
    self.backgroundColor = [UIColor blackColor].CGColor;
    self.frame = CGRectMake(positionX, positionY, 5, 20);
    self.cornerRadius = 5;

    return self;
}

- (id)initEndedWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY {

    self = [CALayer layer];
    self.backgroundColor = [UIColor grayColor].CGColor;
    self.frame = CGRectMake(positionX, (positionY + 7.5), 5, 5);
    self.cornerRadius = 2;
    self.delegate = self;
    return self;
}

- (void) logSomething {
    NSLog(@"It did work!");
}

- (void)reduceSize {

    if (self.frame.size.width > 0 && self.frame.size.height >= 5) {
        self.frame = CGRectMake(self.frame.origin.x, (self.frame.origin.y - 0.5), self.frame.size.width, (self.frame.size.height - 0.5));
        [self setNeedsDisplay];
        NSLog(@"Width: %d", (int)self.frame.size.width);
        NSLog(@"Height: %d", (int)self.frame.size.height);
    } else {
        self.backgroundColor = [UIColor grayColor].CGColor;
        self.frame = CGRectMake(self.frame.origin.x, (self.frame.origin.y + 7.5), 5, 5);
        [animation invalidate];
    }

}

- (void)animate {

    animation = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(reduceSize) userInfo:nil repeats:YES];
}


@end

このクラスを新しいプロジェクトにインポートして、そこで機能しているかどうかを確認しましたが、同じエラーが発生します。これが、クラスのインスタンスのメソッドの1つを呼び出す方法です(すべてのメソッドで同じエラーが発生します)

(クリーンプロジェクトの)mainview.h内:

#import <UIKit/UIKit.h>
#import "SFStripe.h"
#import <QuartzCore/QuartzCore.h>

@interface STViewController : UIViewController {
    SFStripe *stripe;
}
- (IBAction)animate:(id)sender;

@end

インスタンスを作成し、.miでアクションにインスタンスのメソッドを呼び出させます。

#import <QuartzCore/QuartzCore.h>
#import "STViewController.h"
#import "SFStripe.h"

@interface STViewController ()

@end

@implementation STViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    stripe = [[SFStripe alloc] initWithXPosition:30 yPosition:30];
    [self.view.layer addSublayer:stripe];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


}

- (IBAction)animate:(id)sender {

    [stripe animate];

}
@end

すべてのコードについて申し訳ありませんが、私はこの問題の答えを見つけることができず、誰かが私を助けてくれることを願っています!

4

1 に答える 1

0

これは、初期化子を作成する正しい方法ではありません。

- (id)initWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY 
{
    self = [CALayer layer];
    ...

selfサブクラスのインスタンスではなく、プレーンな古いCALayerインスタンスに割り当てています。

self = [super init]代わりに使用して、それがnilでないことを確認してくださいself

于 2012-11-14T04:34:55.670 に答える