Question

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.

Was it helpful?

Solution

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

OTHER TIPS

For any EXC_BAD_ACCESS errors, you are usually trying to send a message to a released object. The BEST way to track these down is use NSZombieEnabled.

This works by never actually releasing an object, but by wrapping it up as a "zombie" and setting a flag inside it that says it normally would have been released. This way, if you try to access it again, it still know what it was before you made the error, and with this little bit of information, you can usually backtrack to see what the issue was.

It especially helps in background threads when the Debugger sometimes craps out on any useful information.

VERY IMPORTANT TO NOTE however, is that you need to 100% make sure this is only in your debug code and not your distribution code. Because nothing is ever released, your app will leak and leak and leak. To remind me to do this, I put this log in my appdelegate:

if (getenv("NSZombieEnabled"))
  NSLog(@"NSZombieEnabled enabled!");

If you need help finding the exact line, Do a Build-and-Debug (CMD-Y) instead of a Build-and-Run (CMD-R). When the app crashes, the debugger will show you exactly which line and in combination with NSZombieEnabled, you should be able to find out exactly why.

Change return value of textFieldShouldReturn to NO.

I had this problem even without resignFirstResponder call when I was displaying a view controller.

You should re-write the hideKeyboard method to:

- (IBAction)hideKeyboard:(id)sender; {
  if ([textfield1 isFirstResponder]) {
    [textfield1 resignFirstResponder];
  }
  if ([textfield2 isFirstResponder]) {
    [textfield2 resignFirstResponder];
  }
  if ([textfield3 isFirstResponder]) {
    [textfield3 resignFirstResponder];
  }
}

Also, make sure that the outlets are all hooked up properly in the nib, and that your file is a UITextFieldDelegate, and the textfields should have their delegate hooked up to file owner in the nib. That should (hopefully) fix the problem.

EDIT: You should try putting the 3 UITextFields in the nib, and hooking them up that way. It usually results in less problems, and it is easier to change the design later if you need to.

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top