尝试在 iOS 中调用 UITextField 子类的自定义委托方法
Trying to call custom delegate method of subclass of UITextField in iOS
我有一个为 UITextField 的子class 创建的自定义委托。在委托 class 中,我声明了一个这样的枚举:
MyCustomDelegate.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "MyCustomTextField.h"
@interface MyCustomDelegate : NSObject <MyDelegate, UITextFieldDelegate>
@property (nonatomic, strong)MyCustomTextField *customTextField;
@end
MyCustomTextField.h:
#import <UIKit/UIKit.h>
typedef enum {
EnumTypeA,
EnumTypeB,
EnumTypeC,
EnumTypeD,
EnumTypeE
} MyEnumType;
@class MyCustomDelegate;
@protocol MyDelegate <NSObject>
@required
- (void)methodA;
- (void)methodB;
@end
@interface MyCustomTextField : UITextField
@property (nonatomic, weak)id <MyDelegate>myDelegate;
@property (nonatomic) MyEnumType enumType;
@end
现在,我正在尝试将此枚举与我项目中其他地方的自定义 UITextField 结合使用,如下所示:
MyViewController.h
#import "MyCustomTextField.h"
#import "MyCustomDelegate.h"
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController
@property (weak, nonatomic) IBOutlet MyCustomTextField *mySampleTextField;
@end
MyViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self.mySampleTextField setMyEnumType:EnumTypeA];
}
但是,我遇到了错误,"No visible @interface for 'MyCustomTextField' declares the selector 'setMyEnumType'"。
谁能看出我做错了什么?
Objective-C 会根据 属性 (enumType
) 的名称自动为您生成 setter 而不是类型的名称 (MyEnumType
).所以你的 setters 应该是下面的:
[self.mySampleTextField setEnumType:EnumTypeA];
我有一个为 UITextField 的子class 创建的自定义委托。在委托 class 中,我声明了一个这样的枚举:
MyCustomDelegate.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "MyCustomTextField.h"
@interface MyCustomDelegate : NSObject <MyDelegate, UITextFieldDelegate>
@property (nonatomic, strong)MyCustomTextField *customTextField;
@end
MyCustomTextField.h:
#import <UIKit/UIKit.h>
typedef enum {
EnumTypeA,
EnumTypeB,
EnumTypeC,
EnumTypeD,
EnumTypeE
} MyEnumType;
@class MyCustomDelegate;
@protocol MyDelegate <NSObject>
@required
- (void)methodA;
- (void)methodB;
@end
@interface MyCustomTextField : UITextField
@property (nonatomic, weak)id <MyDelegate>myDelegate;
@property (nonatomic) MyEnumType enumType;
@end
现在,我正在尝试将此枚举与我项目中其他地方的自定义 UITextField 结合使用,如下所示:
MyViewController.h
#import "MyCustomTextField.h"
#import "MyCustomDelegate.h"
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController
@property (weak, nonatomic) IBOutlet MyCustomTextField *mySampleTextField;
@end
MyViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self.mySampleTextField setMyEnumType:EnumTypeA];
}
但是,我遇到了错误,"No visible @interface for 'MyCustomTextField' declares the selector 'setMyEnumType'"。
谁能看出我做错了什么?
Objective-C 会根据 属性 (enumType
) 的名称自动为您生成 setter 而不是类型的名称 (MyEnumType
).所以你的 setters 应该是下面的:
[self.mySampleTextField setEnumType:EnumTypeA];