netdigger
netdigger

Reputation: 3789

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

Upvotes: 2

Views: 1574

Answers (3)

user3214187
user3214187

Reputation: 41

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.

Upvotes: 1

netdigger
netdigger

Reputation: 3789

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.

Upvotes: 0

Wam
Wam

Reputation: 850

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.

Upvotes: 1

Related Questions