2011-01-20 17 views
0

通常我不會有任何問題添加setter方法到類。obj-c,混淆爲什麼我不能添加setter到這個類?

不過,我試圖添加到庫的使用類,這必須引起問題。

繼承人的I類已將其添加到...

@interface GraphController : TKGraphController { 
UIActivityIndicatorView *indicator; 
NSMutableArray *data; //I've added 
NSString *strChartType; //I've added 
} 
-(void)setContentType:(NSString*)value; //I've added 
@end 

@implementation GraphController 
-(void)setContentType:(NSString*)value { //I've added 

if (value != strChartType) { 
    [value retain]; 
    [strChartType release]; 
    strChartType = value; 
    NSLog(@"ChartType=%@", strChartType); 
}  
} 

繼承人在那裏我得到一個警告..

UIViewController *vc = [[GraphController alloc] init];  
[vc setContentType:myGraphType]; //Warnings on this line see below 
[self presentModalViewController:vc animated:NO]; 
[vc release]; 

myGraphType如果我不斷類中定義。

*警告*

warning: 'UIViewController' may not respond to '-setContentType:' 
warning: (Messages without a matching method signature 

我知道,當你還沒有添加的方法來實施出現的第一個警告。但我有。

我要去哪裏錯了?

回答

5
UIViewController *vc = [[GraphController alloc] init]; 

意味着vc指向的GraphController實例,但變量本身是UIViewController *類型,並且UIViewController不聲明-setContentType:方法。

替換與

GraphController *vc = [[GraphController alloc] init]; 

告訴你與GraphController實例工作的編譯器,它可以識別你的-setContentType:方法。

1

你只要讓編譯器知道你與它知道響應該方法的類的工作。你可以用幾種方法做到這一點,但如果你只是想消除警告,最簡單的方法就是在進行方法調用之前將對象放入一行。

UIViewController *vc = [[GraphController alloc] init];  
[(GraphController *)vc setContentType:myGraphType]; //No warning should appear now. 
[self presentModalViewController:vc animated:NO]; 
[vc release]; 
相關問題