Object C Subclass IPhone UITextField catch Value changed

I have subclasses of UITextField, so I can create some custom behaviors 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 override a ValueChanged event? I don’t seem to catch him, but I try. All I want to do is break one of these Textfields subclasses into a view and catch this event and handle it.

Please, help.

Thanks in advance

+3
source share
2 answers

According to the documentation for UITextFieldDelegateyou can use

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

, .

, delegate UITextField:

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

, , , 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;
} 
-1

Source: https://habr.com/ru/post/1780571/


All Articles