textFieldDidBeginEditing:没有被调用 [英] textFieldDidBeginEditing: not getting called
问题描述
我从 this SO 问题中得到了下面的代码.当我开始编辑时,我试图向上滑动文本字段(因为否则它们会被 iPhone 键盘覆盖).但是,日志语句显示 textFieldDidBeginEditing 方法没有被调用.
I got the code below from this SO question. I'm trying to slide up textfields when I begin editing (because they otherwise get covered by the iPhone keyboard). However, the log statement reveals that the textFieldDidBeginEditing method is not getting called.
我在 UIViewController 的两个不同子类中有下面的代码.例如,在其中一个中,我有一个从故事板连接到 UIViewController 的文本字段,如下所示
I have the code below in two different subclasses of UIViewController. In one of them, for example, I have a textfield connected from the storyboard to the UIViewController like this
@property (strong, nonatomic) IBOutlet UITextField *mnemonicField;
我将文本字段移到视图的顶部(即未被键盘覆盖),以便我可以对其进行编辑以尝试触发日志语句,但它不起作用.文本字段按预期工作,即我输入的数据被保存到 coreData 等.
I moved the textfield up to the top of the view (i.e. not covered by keyboard) so that I could edit it to try to trigger the log statement but it didn't work. The text field otherwise works as expected i.e. the data I enter is getting saved to coreData etc etc.
你能解释一下我可能做错了什么吗?
Can you explain what I might be doing wrong?
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"The did begin edit method was called");
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 180; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
推荐答案
您尚未在 ViewController
类中分配 UITextField
的 delegate
:
You have not assigned your delegate
of UITextField
in your ViewController
class:
在您的 viewcontroller.m 文件中,在 ViewDidLoad
方法中,执行以下操作:
In your viewcontroller.m file, In ViewDidLoad
method, do this:
self.mnemonicField.delegate=self;
在您的 viewcontroller.h 文件中,执行以下操作:
In your viewcontroller.h file, do this:
@interface YourViewController : ViewController<UITextFieldDelegate>
这篇关于textFieldDidBeginEditing:没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!