ios - 委托(delegate)方法声明中的枚举

标签 ios objective-c enums delegates

我在 .h 文件中声明了一个 enum,如下所示:

typedef enum {
   item1 = 0,
   item2,
   item3
} myEnum;

我想在 View Controller 的委托(delegate)方法签名中使用它,如下所示:

@protocol myClassDelegate <NSObject>
- (void)myDelegateMethod:(enum myEnum)type;
@end

我已在此 View Controller 类中包含 .h 文件。

创建上述协议(protocol)时,自动完成功能不建议使用枚举,编译器也会提示。

使用 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

最佳答案

您有一个循环 header 依赖项(ViewControllerA.h 导入 ViewControllerB.h,反之亦然)。

enum 声明移至公共(public)头文件中,并在需要的地方导入:

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

关于ios - 委托(delegate)方法声明中的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27355041/

相关文章:

iphone - 如何移动 Interface Builder 放置的文本字段和按钮

objective-c - 带小数的 float 四舍五入

ios - Swift - 加载外部数据的可变范围

ios - 通过代码在sqlite3中启用foreign_keys=ON

ios - NSTimer每秒更新标签,但覆盖以前的标签

ios - 自定义 Segue 中的 UIKit 动态

ios - 如何删除以前的 ViewController

arrays - Mongoose 枚举字符串数组验证?

具有属性的python枚举

c++ - C 中的目录排序