Reputation: 1012
I'm creating a shopping list app, and trying to implement a custom delegate when editing an item. When creating the @protocol
at the bottom of the header file, when trying to declare a property of that protocol in the @interface
section I'm getting an error of: Cannot find protocol declaration for GEMEditItemViewControllerDelegate
This is what my header file looks like.
#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
Alternatively in a separate instance when declaring the protocol
above the interface
I cannot access the view controller to pass as a parameter for that declaration method.
That header file looks like:
#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
Any thoughts on how to correct this would be greatly appreciated!!
Upvotes: 1
Views: 456
Reputation: 3273
You can do it like this also
@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
Upvotes: 2
Reputation: 605
Your header file should looks like this:
@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
You should not use direct import in your header file. @class
directive is used to prevent cycle dependencies. In your case GEMItem import should be in your meta file.
Upvotes: 0
Reputation: 318814
Try it this way:
@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 - you should move the two picker view protocols from the header file to a class extension in the .m file. The world doesn't need to know this implementation detail.
Upvotes: 0