Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C IPhone Subclassing UITextField catch Value Changed

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

like image 223
IPadHackAndSlash Avatar asked Jun 27 '26 17:06

IPadHackAndSlash


1 Answers

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;
    }
}
like image 56
Reed Olsen Avatar answered Jun 29 '26 05:06

Reed Olsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!