Why UITextField is blocking when setting up for delegation

I have a class that extends a UITextfield. I also have the same set of classes as its own delegate, so when the textbox is selected I can change the background color. As soon as I select the textbox and type a couple of letters, the app blocks 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 .h

@interface MyTextField : UITextField <UITextFieldDelegate>

@end

      

+1


source to share


3 answers


The delegate is always different UIViewController

because the events are delegated to it by another class where the protocol is defined.

No need for delegate methods in the same class when you can access all variables and methods in the same class.

you can just call [self someFunction]

.



Since you inherit from UITextField, you don't even need to define a property for the UITextField delegate. You just need to set it to a different viewController.

Also the class that defines the protocol is only declared and does not conform to the protocol.

The delegate will be a class that conforms to the protocol.

+1


source


Subscribe to UITextFieldTextDidBeginEditingNotification

or UITextFieldTextDidEndEditingNotification

NSNotification

s. In callbacks, check if the notification object is itself. Then follow some steps.



+1


source


The discussion in this question should lead you in the right direction self.delegate = self; What's bad about it?

0


source







All Articles