Reputation: 45
I have a text field named "fieldPassword" declared as an IBOutlet
@property (strong, nonatomic) IBOutlet UITextField *fieldPassword;
I synthesize it later on and then, in an attempt to have the return key dismiss the keyboard, I have:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
fieldPassword.delegate = self;
[fieldPassword resignFirstResponder];
[self.view endEditing:YES];
return YES;
}
The problem is that when I run the simulator and hit the return key in the designated text field, nothing happens. Previously, I also had fieldPassword.delegate = self;
in -viewDidLoad
and that crashed the simulator with an unrecognized selector
error.
Any help is much appreciated!
Upvotes: 0
Views: 95
Reputation: 9589
Follow the below things
1.Go to xib or storyboard where you have set that your view.
2.Right Click the TextField.If you click that you can see the
Outlets->Delegate with Empty Circle
3.Just connect with File's Owner.It is Yellow Color.
Once you do this,the circle is not empty.It is filled now.
4.Then go to declaration or .h part
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
5.Then in .m
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Upvotes: 0
Reputation: 427
It should be [self.fieldPassword resignFirstResponder];
instead of [fieldPassword resignFirstResponder];
Also self.fieldPassword.delegate = self;
should be in viewDidLoad
or viewDidAppear
Upvotes: 1
Reputation: 7479
If you don't set the delegate earlier, you won't get the delegate callback. Try this in viewDidLoad
:
self.fieldPassword.delegate = self;
You might have been missing the self
before.
Upvotes: 0