Reputation: 31
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):
@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *myTextField;
@end
@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;
}
Upvotes: 1
Views: 1289
Reputation: 31
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.
Upvotes: 2
Reputation: 17478
In the nib file, please check that whether you have checked the Auto-enable Return Key check box for the text field.
Upvotes: 0