2

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
netdigger
  • 3,659
  • 3
  • 26
  • 49

3 Answers3

1

I have the exact same problem, which only seems to happen on iOS simulator 5.

It looks like setting the delegate at init makes the app crash.

Simply commenting the self.delegate = self line removes the error.

So I ended up (not very nice, but eh) putting my self.delegate = self in an overloaded - (void) touchesBegan:. This way, I'm sure that my delegate is set when someone starts typing.

Wam
  • 810
  • 1
  • 8
  • 19
1

I have faced this same problem. I finally solved that by adjusting the autocorrectionType.

Please set your textfield to autocorrectionType = UITextAutocorrectionTypeNo;

Hope this solve your problem.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
0

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.

netdigger
  • 3,659
  • 3
  • 26
  • 49