2011-12-17 23 views
0

我有一個UIViewControllerClass Aplha調用另一個UIViewControllerClass Beta在modalView。
Alpha.h,我聲明的函數:使用委託調用調用的函數的ViewController給出錯誤的iOS 5

-(void)selectMyImage:(NSString *)myImage; 

在Aplha.m:

#import "Beta.h" 

-(IBAction)clickMe:(id)sender 
{ 
    Beta *b=[[Beta alloc]initWithNibName:@"Beta" bundle:nil]; 
    b.delegate=self; 
    [self presentModalViewController:b animated:YES]; 
} 

上面的代碼表示的viewController中模態的視圖。我在Beta.h中聲明瞭id delegate;。現在,當我嘗試從Beta類調​​用Alpha的功能時,它給我錯誤。
這裏是Beta.m我的代碼:

-(void)callAlpha 
{ 
    [delegate selectMyImage:imagename]; //imagename is declared in Beta.h too. 
} 

Xcode的顯示紅色的錯誤標記說"No known instance method for selector 'mySelectedImage:'"

在iOS版SDK 4.x中運行良好。在這裏我也有啓用了ARC

回答

1

這是因爲id類型沒有名爲-mySelectedImage:的方法。這是習慣,爲您的委託協議,並宣佈委託如下該協議:

// Declared somewhere that both Alpha and Beta can see 
@protocol BetaDelegate 
- (void)selectMyImage:(NSString*)imageName; 
@end 

// Alpha.h 
@interface Alpha : UIViewController <BetaDelegate> 
// rest of alpha decls 
@end 

// Beta.h 
@interface Beta : UIViewController 
@property(nonatomic,weak) id<BetaDelegate> delegate; 
    // rest of beta decls 
@end 

如果這看起來像的東西太多的工作只是你的工作或什麼的,你也可以只專門聲明代表作爲類Alpha,則編譯器將能夠看到Alpha實際上聲明瞭該方法。

+0

你是指「聲明alpha和beta都能看到的地方」的含義? – 2011-12-17 08:32:32