Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set delegate of text field in Objective-C?

I'd like to customize a UITextField by limiting it to four chars. I'm trying to understand how delegates work in Objective-C and have gone through the following steps to implement this functionality, still with no luck getting a working solution.

1) Created a LimitedLengthTextField objective-c class. Made the class of type UITextField and accept objects of type < UITextFieldDelegate >.

LimitedLengthTextField.h:

@interface LimitedLengthTextField : UITextField <UITextFieldDelegate>
@end

2) Implemented the following method in LimitedLengthTextField.m:

@implementation LimitedLengthTextField

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 4) ? NO : YES;
}

@end

3) Imported "LimitedLengthTextField.h" in my CreateAccount class and tried to set the delegate of the UITextField "ssnTextField" in viewDidLoad as follows (my app accepts the last 4 digits of the user's SSN).

// Set the custom SSN textfield delegate
LimitedLengthTextField *custTextField = [[LimitedLengthTextField alloc] init];
[self.ssnTextField setDelegate:custTextField];

Based on my limited understanding of Objective-C and delegates, I've now created a class, implemented the delegate method I want, then created an instance of that class and assigned it to my UITextView object. What am I missing?

like image 364
Kyle Clegg Avatar asked Aug 06 '12 23:08

Kyle Clegg


People also ask

How do you write a delegate in Objective C?

To create one, you define a class that implements the delegate methods you're interested in, and mark that class as implementing the delegate protocol. Then you could create an instance of MyClass and assign it as the web view's delegate: MyClass *instanceOfMyClass = [[MyClass alloc] init]; myWebView.

What is text field delegate?

A text field calls the methods of its delegate in response to important changes. You use these methods to validate text that was typed by the user, to respond to specific interactions with the keyboard, and to control the overall editing process.

What is a delegate in Swift?

In Swift, a delegate is a controller object with a defined interface that can be used to control or modify the behavior of another object. One example is the UIApplicaitonDelegate in an iOS app.


2 Answers

You should not have subclassed UITextField.  Instead you implement the callbacks in your CreateAccount class.   So you should have something like this:

@interface CreateAccount :UIViewController <UITextFieldDelegate>
// I use UIViewController but whatever your CreateAccount from.

And implement this in your CreateAccount.m file:

This is probably in your viewDidLoad method:

Self.cusTextField.delegate = self;

and this

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 4) ? NO : YES;
}
like image 192
user523234 Avatar answered Sep 30 '22 19:09

user523234


Maybe you should make the CreateAccount (I assume it is a view controller that contains the text field?) conforms UITextFieldDelegate instead of custom text field, and implement shouldChangeCharactersInRange method also in CreateAccount class. Besides, change the delegate to

_ssn.delegate = self;

In this case, maybe you don't need a custom text field at all.

like image 33
Selkie Avatar answered Sep 30 '22 19:09

Selkie