Objective C iPhone 子类化 UITextField catch 值已更改

发布于 2024-10-08 04:41:39 字数 1398 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小姐丶请自重 2024-10-15 04:41:39

根据 UITextFieldDelegate 的文档,您可以使用

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

每当用户在文本字段中键入新字符或删除现有字符时,文本字段都会调用此方法。

确保设置 UITextFielddelegate 属性:

- (id)init
{
    if ((self = [super init]))
    {
        self.delegate = self;
    }
}

According to the documentation for UITextFieldDelegate, you can use

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

The text field calls this method whenever the user types a new character in the text field or deletes an existing character.

Make sure you set the delegate property of your UITextField:

- (id)init
{
    if ((self = [super init]))
    {
        self.delegate = self;
    }
}
黎夕旧梦 2024-10-15 04:41:39

这是我用来只允许某些字符的方法,以及文本字段允许使用多少个字符:

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    /*  allow only these characters in the textField  */   
    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890|:.@~_-+#!{}%*"];
    for (int i = 0; i < [string length]; i++) {
        unichar c = [string characterAtIndex:i];
        if (![myCharSet characterIsMember:c]) {
            return NO;
        }
    }
    /*  choose how many characters the textField can have  */
    NSUInteger newLength = [theTextField.text length] + [string length] - range.length;
    return (newLength > 28) ? NO : YES;
} 

This is what I have used to allow only certain characters, as well as how many characters are allowed for a textField:

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    /*  allow only these characters in the textField  */   
    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890|:.@~_-+#!{}%*"];
    for (int i = 0; i < [string length]; i++) {
        unichar c = [string characterAtIndex:i];
        if (![myCharSet characterIsMember:c]) {
            return NO;
        }
    }
    /*  choose how many characters the textField can have  */
    NSUInteger newLength = [theTextField.text length] + [string length] - range.length;
    return (newLength > 28) ? NO : YES;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文