我有一个名为" fieldPassword"的文本字段。宣布为IBOutlet
@property (strong, nonatomic) IBOutlet UITextField *fieldPassword;
我稍后合成它然后,试图让返回键关闭键盘,我有:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
fieldPassword.delegate = self;
[fieldPassword resignFirstResponder];
[self.view endEditing:YES];
return YES;
}
问题是当我运行模拟器并在指定的文本字段中点击返回键时,没有任何反应。以前,我在fieldPassword.delegate = self;
中也有-viewDidLoad
,并且在模拟器崩溃时出现unrecognized selector
错误。
非常感谢任何帮助!
答案 0 :(得分:1)
应该是[self.fieldPassword resignFirstResponder];
而不是[fieldPassword resignFirstResponder];
同样self.fieldPassword.delegate = self;
应位于viewDidLoad
或viewDidAppear
答案 1 :(得分:0)
如果您之前没有设置代理,则无法获得委托回调。请在viewDidLoad
中尝试此操作:
self.fieldPassword.delegate = self;
您之前可能错过了self
。
答案 2 :(得分:0)
遵循以下内容
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;
}