2011-02-16 30 views
1

(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無法分配,爲什麼我有沒有'-initWit hPNGFileName:andGame:andCGRect:」方法找到

感謝

+0

嗨..你可以發佈'ball'類的`.h`文件嗎? – EmptyStack 2011-02-17 03:45:40

回答

2

從你提供你好像有UIImageView一個實例調用ball的代碼。您確定您的班級被稱爲ball(順便提一下,班級名稱應該以大寫字母開頭),它是.h - 文件是否包含在內?

一些背景信息:alloc是一個類的方法,你不把它發送到一個實例 - 它的簽名是類似+ (id) alloc;(這裏介意加!)基本上,警告說,你嘗試調用實例方法分配在你的對象上,如果它存在的話(這很可能不是這種情況)將會有簽名- (id) alloc;(注意這裏減號!)。因此,編譯器將ball識別爲類名稱,但作爲UIImageView的實例,如警告所示 - 它可能不會響應- (id) alloc;。 默認情況下,編譯器假定未知方法返回id(而不是您預期的ball)的實例,這就是爲什麼它警告您無法找到此對象的名爲-initWithPNGFileName:andGame:andCGRect的方法。

+0

.h已包含在內。 UIImageView被命名爲類會產生一個問題? – Michele 2011-02-16 21:25:38