分享

Assigning to 'id<Delegate>' from incompatible type 'ViewController *const_strong'

 没原创_去搜索 2015-08-20

Assigning to 'id<Delegate>' from incompatible type 'ViewController *const_strong'




Throughout my app, I'm getting semantic issue warnings when I set ViewController.delegate = self. I have searched and found similar posts but none were able to solve my problem.

ViewController.m:

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

I get the error message when setting .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

Can anyone point me in the right direction to resolve the warning messages?

shareimprove this question

At this line :

gameAddViewContoller.delegate=self; 

Notice that self is of type ViewController which does NOT conform to the GameAddViewController protocol. 

shareimprove this answer
1 
What do I need to do to fix this. Is there something I need to change in the GameAddViewController protocol? –  David L Mar 25 '12 at 21:30
   
Well, it depends on who you want to be your delegate. Who is implementing the GameAddViewController protocol –  giorashc Mar 26 '12 at 7:39
   
GameAddViewController is the child of ViewController and I am implementing the gameAddViewControllerDidCancel method in ViewController. The error message is from the prepareForSegue method in ViewController when I set gameAddViewContoller.delegate=self. –  David LMar 26 '12 at 23:51
1 
So do as @borrrden suggested. Conform the ViewController to the GameAddViewControllerDelegate delegate. –  giorashc Mar 27 '12 at 7:25
21 
OK, so I finally got it. I added this to my header file //#import "GameAddViewController.h" @interface ViewController : UIViewController <GameAddViewControllerDelegate> @end// –  David L Mar 28 '12 at 4:25

For me what ended up happening is that I wasn't adding the delegate to the @interface on my header file

For example

@interface TheNameOfYourClass : UIViewController <TheDelegatorClassDelegate>

@end
shareimprove this answer

You are putting the < GameAddViewControllerDelegate > in the wrong place. It doesn't go on GameAddViewController, it goes on ViewController.

shareimprove this answer

This might help other people who are adding Multipeer Connectivity straight to a ViewController. At the top of myViewControllerName.h add '<MCSessionDelegate>':

@interface myViewControllerName : UIViewController<MCSessionDelegate>
shareimprove this answer





====================

环境:Xcode 6.3.1,OSX 10.3.3, Deployment Target 8.0


在一个ViewController中添加一个UIWebView,在下面这一行时报warning。

self.webView.delegate = self;

解决办法是在xxxViewController.h文件中加<UIWebViewDelegate>,如下所示

@interface FeedBackViewController : UIViewController<UIWebViewDelegate>

原因是,self想要代理webView时,需要实现webView相关的protocol。


参考资料:http://www./when_assign_delegate_occur_warning_assigning_to_id_uinavigationcontrollerdelegate_uiimagepickercontrollerdelegate_from_incompatible_type_const_strong/



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多