1

I'm using iOS 5 and trying to use TextField with UITextFieldDelegate, it's worked exactly like I want (but JUST in the first View). I don't understand, why it's not working in the next view.

For simple example, I created new project (ViewController). There I added one button, that connect to another view (SecondViewController). In the SecondViewController, I have one TextField. With this textField I want to use textFieldShouldReturn. But it seems, that this method is not being called. What I know, I should write the delegate in ViewDidLoad. Should I write myTextField.delegate = self; ? but I think something wrong there. I used Debugging, and always at that position, i'm getting problem. Could you please tell me, what the problem is? and how can i solve it ;(

Thanks in advance.....

Here is my code (that it works, in the first view (ViewController). Unfortunately here not (SecondViewController):


SecondViewController.h

@interface SecondViewController : UIViewController<UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField *myTextField;

@end

SecondViewController.m

@implementation SecondViewController

@synthesize myTextField;


- (void)viewDidLoad{

    [super viewDidLoad];

    myTextField.delegate = self; // here i get the problem
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{   // this method is not being called


    [textField resignFirstResponder];
    NSLog(@"is called!");
    NSString *valueMyTextField = myTextField.text;
    NSLog(@"%@", valueMyTextField);

    return YES;
}
NitNot
  • 31
  • 4

2 Answers2

2

Problem solved... :) The problem was the connection from firstView to secondView. Do not use addSubView, if you want to add Controller! Use presentModalViewController :) Hope it helps, in case you have the same problem like me.

NitNot
  • 31
  • 4
0

In the nib file, please check that whether you have checked the Auto-enable Return Key check box for the text field.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • thanks for the replay :) It was not checked, and i have checked the check box,but it doesn't work (like before) ;( – NitNot Nov 21 '11 at 10:28
  • 1
    Also try setting the delegate through nib itself by setting the delegate to the file owner, considering the file owner is SecondViewController.. – Ilanchezhian Nov 21 '11 at 10:45
  • do you mean, instead textField.delegate = self; i should set the delegate through nib. from textField into file owner, right? i have done before, but it's not working :( it's confusing, i think the delegate address is not correct... but where should i delegate, i didn't know... if i make directly in the first view controller, i don't have the problem. please help me... ;( – NitNot Nov 21 '11 at 10:50