初始化自定义类的问题(UIImageView可能无法响应'-alloc')

时间:2011-02-16 20:58:36

标签: objective-c uiimageview alloc

我对这一行有一个问题

ball* balle = [[ball alloc] initWithPNGFileName:ballPath andGame:game andCGRect:CGRectMake( 200, 100, 16, 16 )] ;

问题是“未声明的球”并发出警告:'UIImageView'可能无法响应'-alloc'并发出警告:没有'-initWithPNGFileName:andGame:andCGRect:'找到方法

以下是方法:

-(id) initWithPNGFileName:(NSString *) filename andGame: (Game*) game andCGRect: (CGRect) imagerect_ {  
    [super init];

    CGDataProviderRef provider;
    CFStringRef path;
    CFURLRef url;


    const char *filenameAsChar = [filename cStringUsingEncoding:[NSString defaultCStringEncoding]]; //CFStringCreateWithCString n'accepte pas de NSString

    path = CFStringCreateWithCString (NULL, filenameAsChar, kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, NO);
    CFRelease(path);
    provider = CGDataProviderCreateWithURL (url);
    CFRelease (url);
    image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault);

    CGDataProviderRelease (provider);

    imageRect = imagerect_ ;

    [game addToDraw: self] ;
    [game addToTimer : self] ;

    return self ;

}


-(void) draw: (CGContextRef) gc
{
    CGContextDrawImage (gc, imageRect, image );
}

我不明白为什么UIImageView无法分配,为什么我没有'-initWithPNGFileName:andGame:andCGRect:'找到方法

由于

1 个答案:

答案 0 :(得分:2)

从您提供的代码中可以看出,UIImageView的实例名为ball。您确定您的班级被称为ball(顺便说一下,班级名称应以大写字母开头)并且.h - 文件是否包含在内?

一些背景信息:alloc是一个类方法,你不会将它发送给一个实例 - 它的签名就像+ (id) alloc;(注意这里的加号!)基本上,警告说你试图在你的对象上调用实例方法 alloc,如果它存在的话,它将具有签名- (id) alloc;(请注意这里的减号!)(很可能不是这种情况)。因此,编译器不会将ball识别为类名,而是将其视为UIImageView的实例,如警告所示 - 可能不会响应- (id) alloc;。 默认情况下,编译器假定未知方法返回id的实例(而不是预期的ball实例),这就是为什么它会警告您无法找到名为{{1的方法这个对象。

相关问题