1

I have a class that extends UITextfield. I also have the same class set to be it's own delegate so when the text field is selected I can change the background color. Once I selecte the text field and type a couple of letters the app locks up and crashes.

here is what my .m file looks like

@implementation MyTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.delegate = self;

    }
    return self;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"run did begine editing");
    [self setBackgroundColor:[UIColor colorWithRed:0.204 green:0.239 blue:0.275 alpha:0.25]];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"run did end editing");
    [self setBackgroundColor:[UIColor clearColor]];
}

here is the .h

@interface MyTextField : UITextField <UITextFieldDelegate>

@end
mattwallace
  • 4,132
  • 6
  • 43
  • 77

3 Answers3

1

The delegate is always another UIViewController as the events are delegated to it by another class where protocol is defined.

There's no need of delegate methods in the same class when you can access all the variables and methods in the same class.

you simply can call [self someFunction].

As you are inheriting the UITextField , you don't need to define even a property for the UITextField delegate.You just need to set it a different viewController.

Also the class defining the protocol just has the declaration and it does not conform to the protocol.

The delegate will be the class which conforms to the protocol.

Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
1

Subscribe to UITextFieldTextDidBeginEditingNotification or UITextFieldTextDidEndEditingNotification NSNotifications. In callbacks check if notification object is self. Then perform some action on it.

DD_
  • 7,230
  • 11
  • 38
  • 59
Jeepston
  • 1,361
  • 7
  • 7
0

The discussion in this question should guide you in the right direction self.delegate = self; what's wrong in doing that?

Community
  • 1
  • 1
Praveen S
  • 10,355
  • 2
  • 43
  • 69