3

I have a simple piece of code that works on every system except iOS4. It's about resigning first responder for a UITextField. The text field is all wired up, and here's the code for the delegate:

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [opis resignFirstResponder];
    return YES;
}

I've tried it almost every possible way, trying to return NO as well and it doesn't work. The weirdest thing is that it DOES work on every iphone OS before 4.0 and on iPad as well (tested in simulators and on actual devices) Can anyone help?

dusker
  • 580
  • 3
  • 11
  • Why not use [textField resignFirstResponder];? Maybe your opis variable is not set up correctly. – Eiko Jul 06 '10 at 12:20
  • Tried it as well - doesn't work. – dusker Jul 06 '10 at 12:21
  • Thats what i too will suggest try using [textField resignFirstResponder]; or check out that the textfield is properly allocated and is the same textfield that u r trying to resign.. Happy Coding... – Suresh Varma Jul 06 '10 at 12:23
  • It is all properly allocated and wired up with outlets. Tried [textField resignFirstResponder] also but it doesn't work. – dusker Jul 06 '10 at 12:25
  • Have you tried setting a breakpoint on the textFieldShouldReturn method? This would determine if the problem lies with the calling of the delegate method or alternatively the text field refusing to resign first responder status. – Sean Patrick Murphy Jul 06 '10 at 13:12
  • Can you reduce this to a simple test case? – tc. Jul 06 '10 at 15:20

3 Answers3

4

This worked with my 3GS in iOS4. Are you sure you properly set the UITextField delegate?

- (BOOL) textFieldShouldReturn:(UITextField*)textField {
[textField resignFirstResponder];
return YES;
}
hypercrypt
  • 15,389
  • 6
  • 48
  • 59
Charter
  • 783
  • 4
  • 6
  • Nope I really meant did you set your class implementing the UITextFieldDelegate protocol as the delegate for the UiTextField outlet. – Charter Jul 06 '10 at 13:17
  • In your answer you've stated TextVIEWDelegate. – dusker Jul 06 '10 at 13:44
  • The only time I've seen this happen is when the UITextField's delegate property is not set to the File's Owner. Why are you calling [opis resignFirstResponder]? Do you mean `if (textField == opis) [textField resignFirstResponder]`? – Christopher Pickslay Jul 07 '10 at 06:21
1

Not sure if this is similar to your case, but I have a UITextField in a UIAlertView and HAD a similar problem with keyboard not dismissing on resignFirstResponder.

Here was my solution:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    [alert resignFirstResponder];
    return YES;
}

It seems that the alert became firstResponder after textfield resigned.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
0

This is a very classic problem. I assume you are setting the delegate to your textField but the question is where? Are you setting in the initWithNib... initializer of your controller? Than that is wrong. Because your text field is not initialized by then. Its initialize in viewDidLoad method, so you should set your delegate there and it should work.

theiOSguy
  • 608
  • 6
  • 18