2012-03-25 19 views
69

Trong suốt ứng dụng của tôi, tôi nhận được cảnh báo vấn đề ngữ nghĩa khi đặt ViewController.delegate = self. Tôi đã tìm kiếm và tìm thấy các bài viết tương tự nhưng không ai có thể giải quyết vấn đề của tôi.Chỉ định cho 'id <Delegate>' từ loại không tương thích 'ViewController * const_strong'

ViewController.m:

GameAddViewController *gameAddViewContoller = [[navigationController viewControllers] objectAtIndex:0]; 
gameAddViewContoller.delegate=self; 

Tôi nhận được thông báo lỗi khi thiết .delegate=self.

GameAddViewController.h:

@protocol GameAddViewControllerDelegate <NSObject> 

- (void)gameAddViewControllerDidCancel:(GameAddViewController *)controller; 
- (void)gameAddViewController:(GameAddViewController *)controller didAddGame:(Game *) game; 

@end 

@interface GameAddViewController : UITableViewController <GameAddViewControllerDelegate> 
{ 
sqlite3   *pitchcountDB; 
NSString  *dbPath; 
} 
@property (nonatomic, strong) id <GameAddViewControllerDelegate> delegate; 
... 
@end 

ViewController.h:

#import "GameAddViewController.h" 

@class ViewController; 
@protocol ViewControllerDelegate <NSObject> 
- (void)ViewControllerDidCancel:(ViewController *)controller; 

@end 
@interface ViewController : UIViewController <ViewControllerDelegate> 
-(void) checkAndCreateFile; 
@end 

bất cứ ai có thể chỉ cho tôi đi đúng hướng để giải quyết các thông điệp cảnh báo?

Trả lời

69

Tại dòng này:

gameAddViewContoller.delegate=self; 

ý rằng tự là loại ViewController mà KHÔNG phù hợp với các giao thức GameAddViewController.

+1

Tôi cần phải làm gì để khắc phục điều này. Có cái gì tôi cần phải thay đổi trong giao thức GameAddViewController? –

+0

Vâng, nó phụ thuộc vào người bạn muốn làm đại biểu của bạn. Ai đang thực hiện giao thức GameAddViewController – giorashc

+0

GameAddViewController là con của ViewController và tôi đang triển khai phương thức gameAddViewControllerDidCancel trong ViewController. Thông báo lỗi là từ phương thức prepareForSegue trong ViewController khi tôi đặt gameAddViewContoller.delegate = self. –

12

Bạn đang đặt < GameAddViewControllerDelegate> ở địa điểm sai. Nó không đi vào GameAddViewController, nó đi trên ViewController.

54

Đối với tôi những gì đã kết thúc xảy ra là tôi đã không thêm các đại biểu đến @ interface trên tập tin tiêu đề của tôi

Ví dụ

@interface TheNameOfYourClass : UIViewController <TheDelegatorClassDelegate> 

@end 
+0

Nó làm việc từ tôi, Upvoted !!! – NSPratik

3

này có thể giúp những người khác đang thêm Multipeer Khả năng kết nối thẳng đến ViewController. Ở phía trên của myViewControllerName.h thêm '<MCSessionDelegate>':

@interface myViewControllerName : UIViewController<MCSessionDelegate> 
0

cũng có, nếu bạn định nghĩa đại biểu của bạn trên xx.m, nhưng bạn sử dụng nó trong lớp khác. bạn có thể gặp phải vấn đề này. vì vậy, chỉ cần đặt giao thức xác định trên xx.h, khi nó là cần thiết.

Các vấn đề liên quan