[英]Enums in delegate method declaration
我在.h
文件中声明了一个enum
,如下所示:
typedef enum {
item1 = 0,
item2,
item3
} myEnum;
我想在视图控制器的委托方法签名中使用它,如下所示:
@protocol myClassDelegate <NSObject>
- (void)myDelegateMethod:(enum myEnum)type;
@end
我已经在该视图控制器类中包含了.h
文件。
创建上述协议时,自动完成功能不建议枚举,并且编译器也会抱怨。
使用int
代替签名中的枚举可以很好地工作。 但是,我想知道是否有/没有使用枚举的方式,或者我做错了什么。
我看过很多帖子,但所有帖子都是正常方法。
编辑:
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
您具有循环头依赖项( ViewControllerA.h
导入ViewControllerB.h
,反之亦然)。
将enum
声明移到公共头文件中,然后将其导入到任何需要的地方:
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
这是一个演示您的视图控制器结构的示例,它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.