Search code examples
ios5uitextfieldios-simulatoruistoryboarduitextfielddelegate

iPhone Simulator crash (with EXC_BAD_ACCESS) when UITextField delegator is called


So I've got some textfield laid up in my controller..

The Controller extends UITextFieldDelegate so it handlers textFieldDidBeginEditing for those textfields, and all that works fine!

Then I tried to added a new textfield from a new class called TestTextField.

I changed the custom class in the storyboard to TestTextField and implemented the following way: (what happens is that the simulator starts and NSLog prints "init!" and then when I press the TestTextField NSLog prints "begin" and after that the simulator stops with EXC_BAD_ACCESS.)

TestTextField.m

#import "TestTextField.h"

@implementation TestTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id) initWithCoder:(NSCoder *) coder
{
    self = [super initWithCoder:coder];
    NSLog(@"init!");
    self.delegate = self;
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"begin");
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"end ");

}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    return YES;
}

@end

TextField.h

#import <UIKit/UIKit.h>

@interface TestTextField : UITextField <UITextFieldDelegate>

@end

Solution

  • By now I am pretty sure what happend, since this is a long time ago.

    I'm 99% certain that this problem was due to leaving the "Custom class" value in XCode with some old value that didnt exist anymore.