I have an enum
declared in the .h
file as follows:
typedef enum {
item1 = 0,
item2,
item3
} myEnum;
I want to use it in a delegate method signature in a view controller as follows:
@protocol myClassDelegate <NSObject>
- (void)myDelegateMethod:(enum myEnum)type;
@end
I have included the .h
file in this view controller class.
Autocomplete does not suggest the enum when creating the above protocol and the compiler complains as well.
It works fine with using int
instead of the enum in the signature. However, I'd like to know if there is/isn't a way of using the enum or if I'm doing something wrong.
I have gone through many posts but all of them were of normal methods.
EDIT:
ViewControllerA.h
#import <UIKit/UIKit.h>
#import "ViewControllerB.h"
typedef enum {
item1 = 0,
item2,
item3
} myEnum;
@interface ViewControllerA : UIViewController <myClassDelegate>
@end
ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
@protocol myClassDelegate <NSObject>
- (void)myDelegateMethod:(enum myEnum)type; // Autocomplete does not suggest the enums
// Also, x-code gives warning: Declaration of 'enum myEnum' will not be visible outside of this functio
@end
@interface ViewControllerB : UITableViewController
@property (nonatomic, strong) id<myClassDelegate> delegate;
@end
You have a circular header dependency (ViewControllerA.h
imports ViewControllerB.h
and vice versa).
Move the enum
declaration into a common header file and import that wherever it's needed:
CommonTypes.h:
typedef enum {
item1,
item2,
item3
} MyEnum;
ViewControllerA.h:
#import <UIKit/UIKit.h>
#import "ViewControllerB.h"
@interface ViewControllerA : UIViewController <myClassDelegate>
// I assume there is a reference to ViewControllerB here somewhere?
@end
ViewControllerB.h:
#import <UIKit/UIKit.h>
#import "CommonTypes.h"
@protocol myClassDelegate <NSObject>
- (void)myDelegateMethod:(MyEnum)type;
@end
@interface ViewControllerB : UITableViewController
@property (nonatomic, strong) id<myClassDelegate> delegate;
@end
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments