首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >声明委托时遇到的麻烦

声明委托时遇到的麻烦
EN

Stack Overflow用户
提问于 2012-03-03 00:28:32
回答 4查看 392关注 0票数 0

我有两个视图,其中view1调用view2。我需要将数据从view2传递回view1。因此,我正在尝试设置一个代理。下面是我在视图控制器2中得到的内容:

代码语言:javascript
复制
.h file

@protocol addEventDelegate <NSObject>

-(void) setAddedEventFlag:(BOOL) hasAddedEvent;

@end

@interface AddEventViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDelegate>

@property (weak, nonatomic) id delegate;  //changed from strong to weak

然后,我在.m文件中@synthesize委托

当尝试包含第一个视图控制器的addEventDelegate时,xcode找不到它:

代码语言:javascript
复制
.h file

#import "AddEventViewController.h"

@interface FieldReportViewController : UIViewController <UITextFieldDelegate, 
UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDelegate, addEventDelegate>

我得到这样的错误:“找不到‘addEventDelegate’的协议声明”。

怎么啦?

编辑:

//代码

错误:

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-03-09 04:07:34

解决了问题。我有一个#import循环。我正在#导入我的.h文件中的所有类。我在.h文件中更改为@Class,并将#import的移动到.m文件中,现在效果很好。

票数 0
EN

Stack Overflow用户

发布于 2012-03-03 00:33:56

  1. 确保拼写正确。
  2. 确保将AddEventViewController.h/.m添加到项目中。

除此之外,你所拥有的一切都很好。

编辑

我还建议重命名你的代理,可能会有命名冲突。虽然我没有看到'add‘和'set’有任何问题,但我在过去看到过以'new‘为前缀的问题。

此外,清理您的项目,并重新构建,看看这是否有帮助。

如果您的项目中有多个目标,并且可能只向其中一个添加了AddEventViewController.h/m,而您正在构建/调试另一个目标,则可能会出现这种情况。

票数 0
EN

Stack Overflow用户

发布于 2012-03-03 03:32:33

下面是定义协议的正确方法

代码语言:javascript
复制
@protocol addEventDelegate; // forward declaration for delegate property

@interface AddEventViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDelegate>
{
    id <addEventDelegate> *delegate
}

@property (weak, nonatomic) id <addEventDelegate> *delegate; 

@end // interface

@protocol addEventDelegate <NSObject>
// @optional // if you want to make it optional
    -(void) setAddedEventFlag:(BOOL) hasAddedEvent;
@end // protocol
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9536800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档