自定义委托找不到声明

时间:2014-04-16 05:27:49

标签: ios objective-c delegates

我正在创建购物清单应用,并尝试在编辑商品时实施自定义代理。在头文件底部创建@protocol时,在@interface部分尝试声明该协议的属性时,我收到错误:Cannot find protocol declaration for GEMEditItemViewControllerDelegate

这就是我的头文件的样子。

#import <UIKit/UIKit.h>
#import "GEMItem.h"

@interface GEMEditItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property GEMItem *item;
@property (weak) id<GEMEditItemViewControllerDelegate> delegate;

@end

@protocol GEMEditItemViewControllerDelegate <NSObject>
@required
- (void)controller:(GEMEditItemViewController *)controller didUpdateItem:(GEMItem *)item;
@end // End of delegate protocol

或者在单独的实例中,在protocol上方声明interface时,我无法访问视图控制器以作为该声明方法的参数传递。

该头文件如下所示:

#import <UIKit/UIKit.h>
#import "GEMItemManager.h"

@protocol GEMAddItemViewControllerDelegate <NSObject>
/*
// Tried to add the controller (controller:(GEMAddItemviewController *)controller) as first paramiter, but was getting and errror, so I have omitted it for the time being
- (void)controller:(GEMAddItemViewController *)controller didSaveItemWithName:(NSString *)name andQuantity:(float)quantity andPrice:(float)price andCategory:(NSString *)category andNotes:(NSString *)notes;
*/

- (void)didSaveItemWithName:(NSString *)name andQuantity:(float)quantity andPrice:(float)price andCategory:(NSString *)category andNotes:(NSString *)notes;

@end

@interface GEMAddItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak) id<GEMAddItemViewControllerDelegate> delegate;

@property NSArray *categories;

@end

对于如何纠正这一点的任何想法都将非常感激!!

3 个答案:

答案 0 :(得分:2)

你也可以这样做

@protocol GEMEditItemViewControllerDelegate;
@interface GEMEditItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property GEMItem *item;
@property (weak) id<GEMEditItemViewControllerDelegate> delegate;

@end

@protocol GEMEditItemViewControllerDelegate <NSObject>
@required
- (void)controller:(GEMEditItemViewController *)controller didUpdateItem:(GEMItem *)item;
@end

答案 1 :(得分:0)

以这种方式尝试:

@class GEMEditItemViewController; // forward declaration of class

@protocol GEMEditItemViewControllerDelegate <NSObject>
@required
- (void)controller:(GEMEditItemViewController *)controller didUpdateItem:(GEMItem *)item;
@end // End of delegate protocol

@interface GEMEditItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property GEMItem *item;
@property (weak) id<GEMEditItemViewControllerDelegate> delegate;

@end

BTW - 你应该将两个选择器视图协议从头文件移动到.m文件中的类扩展。世界并不需要知道这个实现细节。

答案 2 :(得分:0)

您的头文件应如下所示:

@class GEMEditItemViewController;
@class GEMItem;

@protocol GEMEditItemViewControllerDelegate <NSObject>
@required
- (void)controller:(GEMEditItemViewController *)controller didUpdateItem:(GEMItem *)item;
@end // End of delegate protocol

@interface GEMEditItemViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property GEMItem *item;
@property (weak) id<GEMEditItemViewControllerDelegate> delegate;

@end

您不应在头文件中使用直接导入。 @class指令用于防止循环依赖。在您的情况下,GEMItem导入应该在您的元文件中。