【问题标题】:Trouble with error 'cannot find protocol declaration'错误“找不到协议声明”的问题
【发布时间】:2012-02-24 04:22:19
【问题描述】:

我也遇到了与此相同的问题,我尝试了解决方案,但它仍然不适合我,所以再次问这个问题。

这是我的VCWithProtocol.h

@protocol mydemoDelegate
@optional
-(void)demoDelegateMethodWithSuccess:(BOOL)yesOrNo;
@end

@interface VCWithProtocol : UIViewController 
{ 
   id<mydemoDelegate>mydelegate;
}
@property (nonatomic,assign)id<mydemoDelegate>mydelegate;

VCWithProtocol.m

`@synthesize mydelegate`

这是我尝试使用我的代表的课程

@class VCWithProtocol;

@interface VCTOUseDelegate : UIViewController <mydemoDelegate> //here is where it shows error with cannot find protocol declaration

VCTOUseDelegate.m

VCWithProtocol *obj = [[VCWithProtocol alloc] init];
obj.mydelegate = self;

我尝试导入 VCWithProtocol,但效果不佳

【问题讨论】:

  • 什么不起作用?你没有向我们展示足够的东西来告诉我们什么是错的。是的,您应该 `#import "VCWithProtocol.h" @class 指令不导入标头。
  • 我应该两者都做吗? #import "VCWithProtocol.h"@class VCWithProtocol.h
  • 不只是导入。并告诉我们为什么它不起作用并向我们展示VCTOUseDelegate 的实现
  • 我更新问题请检查
  • 当我在 .h 文件中声明它时出现问题,那么我将如何使用该委托的方法

标签: iphone objective-c ios4 delegates protocols


【解决方案1】:

而不是@class VCWithProtocol;#import VCWithProtocol.h

还有一些好的做法 使用大写字母作为任何类名和协议的第一个字符。
例如MyDemoDelegate 并避免保留委托使用

@property (nonatomic,assign)id<mydemoDelegate>mydelegate;

【讨论】:

  • 我用#import 尝试了你的解决方案,但问题仍然存在。在 UIViewController
【解决方案2】:

合成你的mydelegate,然后调用

[self mydelegate];

【讨论】:

  • 是的,我合成了它,也合成了VCTOUseDelegate.mydelegate = self;
【解决方案3】:

您需要将 #import "VCWithProtocol.h" 添加到 .m 文件的顶部。

【讨论】:

  • 需要在头文件中。
  • 否,转发使用@protocol mydemoDelegate 声明协议;在头文件中。
  • 但是为什么在标头中使用@protocol,然后在实现文件中使用#import,当您可以在标头中使用#import 并完成时?
  • 如果这样做,每次更改导入的协议时,编译器都必须重新编译该类(以及#imports 它的每个类)。如果你在任何地方都这样做,那么编译速度会变慢,因为每次更改头文件时都需要重新编译一半的应用程序。
  • Inder Kumar Rathore 建议我不要使用@class 和#import the class
【解决方案4】:
@property (nonatomic,**weak**)id<mydemoDelegate>mydelegate;

代理总是需要薄弱环节

【讨论】:

  • 大多数时候,并非总是如此。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
相关资源
最近更新 更多