Objective C iPhone 子类化 UITextField catch 值已更改
我对 UITextField 进行了子类化,因此我可以为其创建一些自定义行为。以下是我的类:
DataboundTextField.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "TestEntities.h"
@interface DataboundTextField : UITextField <UITextFieldDelegate> {
NSString *valueMember;
NSString *displayValueMember;
ODataObject *boundEntity;
}
@property (nonatomic, retain) NSString *valueMember;
@property (nonatomic, retain) NSString *displayValueMember;
@property (nonatomic, retain) ODataObject *boundEntity;
-(void)SetupDataBinding:(ODataObject*)oDataEntity ValueMember:(NSString*)valMemberID DisplayValueMember:(NSString*)disValMember;
@end
DataboundTextField.m
#import "DataboundTextField.h"
@implementation DataboundTextField
@synthesize valueMember;
@synthesize displayValueMember;
@synthesize boundEntity;
-(id) initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder]))
{
self.delegate = self;
}
return self;
}
-(void)SetupDataBinding:(ODataObject*)oDataEntity ValueMember:(NSString*)valMemberID DisplayValueMember:(NSString*)disValMember{
}
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
@end
如何覆盖 ValueChanged 事件?无论我怎么努力,我似乎都无法抓住它。我想做的就是在视图上添加这些子类文本字段之一并捕获该事件并处理它。
请帮忙。
提前致谢
I have subclassed UITextField so I can create some custom behaviours for it. Here are my classes:
DataboundTextField.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "TestEntities.h"
@interface DataboundTextField : UITextField <UITextFieldDelegate> {
NSString *valueMember;
NSString *displayValueMember;
ODataObject *boundEntity;
}
@property (nonatomic, retain) NSString *valueMember;
@property (nonatomic, retain) NSString *displayValueMember;
@property (nonatomic, retain) ODataObject *boundEntity;
-(void)SetupDataBinding:(ODataObject*)oDataEntity ValueMember:(NSString*)valMemberID DisplayValueMember:(NSString*)disValMember;
@end
DataboundTextField.m
#import "DataboundTextField.h"
@implementation DataboundTextField
@synthesize valueMember;
@synthesize displayValueMember;
@synthesize boundEntity;
-(id) initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder]))
{
self.delegate = self;
}
return self;
}
-(void)SetupDataBinding:(ODataObject*)oDataEntity ValueMember:(NSString*)valMemberID DisplayValueMember:(NSString*)disValMember{
}
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
@end
How do I go about overriding the ValueChanged event? I cannot seem to catch it however I try. All I want to do is wack one of these subclassed Textfields on a view and catch that event and handle it.
Please help.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据
UITextFieldDelegate
的文档,您可以使用- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
确保设置
UITextField
的delegate
属性:According to the documentation for
UITextFieldDelegate
, you can use- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Make sure you set the
delegate
property of yourUITextField
:这是我用来只允许某些字符的方法,以及文本字段允许使用多少个字符:
This is what I have used to allow only certain characters, as well as how many characters are allowed for a textField: