【问题标题】:Cant call methods on instance无法在实例上调用方法
【发布时间】:2012-11-02 14:33:25
【问题描述】:

在做一个项目时,我创建了一个类,我在另一个类中实例化(没什么特别的),但是如果我尝试为这个实例调用一个方法,我会得到:

由于未捕获的异常'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

我将这个类导入到一个新项目中只是为了看看它是否在那里工作,但得到了同样的错误。这是我为类的实例调用其中一种方法的方式(所有方法都出现相同的错误)

在 mainview.h(干净项目的)中:

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

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

@end

我创建了实例并在 .m 中让操作调用实例的方法:

#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

对不起所有的代码,但我找不到这个问题的答案,希望有人能帮助我!

【问题讨论】:

  • 我创建了“logSomething”方法只是为了看看我的方法是否有问题,但得到了同样的错误!
  • 尝试合成stripe。另外,只需将 NSLog 语句放在您的 animate 方法中

标签: objective-c animation methods call instance


【解决方案1】:

这不是编写初始化程序的正确方法:

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

您将 self 分配为普通的旧 CALayer 实例,而不是您的子类的实例。

改用self = [super init],然后检查self 不为零。

【讨论】:

  • 好的,看到我的错误后 :D 非常感谢,我正在寻找几个小时!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
相关资源
最近更新 更多