I have a textfield that i want to hide when the user presses the return button. the textfield was created in the interface builder, i added the textfield delegate in my .h file, and set the delegate for the textfield as the file owner.
@interface ProfileEdit : UIViewController<UITextFieldDelegate>{
UITextField *textfield1;
UITextField *textfield2;
UITextField *textfield3;
}
- (void)viewDidLoad
{
textfield1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 49, 164, 31)];
[textfield1 setDelegate:self];
[textfield1 setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.view addSubview:textfield1];
textfield2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 124, 164, 31)];
[textfield2 setDelegate:self];
[textfield2 setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.view addSubview:textfield2]
textfield3 = [[UITextField alloc] initWithFrame:CGRectMake(20, 198, 164, 31)];
[textfield3 setDelegate:self];
[textfield3 setAutocorrectionType:UITextAutocorrectionTypeNo];
[self.view addSubview:textfield3];
[super viewDidLoad];
}
I also put a button in the background, where the touchupinside event triggers
-(IBAction)hideKeyboard:(id)sender{
[textfield1 resignFirstResponder];
[textfield2 resignFirstResponder];
[textfield3 resignFirstResponder];
}
This works fine, no errors. But for this
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self hideKeyboard:nil];
return YES;
}
I get the EXC_BAD_ACCESS in the main.m. I’ve been stuck on this for a couple days and have no idea why this is happening.
I tested this on my iphone and there was no error. I think it has to do with the simulator itself. I found that if i put a delay on the
[testField resignFirstResponder], no error is thrown in the simulator