如何在 if 条件下对所有检查 UITextField 执行检查 [英] How can I perform a check on all check UITextField's in if condition
问题描述
我一直在寻找解决方案,但目前还没有找到,请参考这个,我想知道是否有一种方法可以对所有 UITextField 执行检查,而不是使用硬编码值,谢谢您的帮助.
I have been looking out for solutions but haven't found one so far, please refer to the answer to this, i want to know if there is a way i can perform the check on all UITextFields, instead of having a hard coded value, thanks for help.
推荐答案
首先确保所有 textField 的委托都设置为 self(意味着您的 viewController)
first your make sure that all textField have their delegate set to self ( means your viewController)
Ex. [myTextField setDelegate:self];// you can also set the delegate in Storyboard or xib directly
然后在类实现中添加一个实例变量,例如 -
Then add a instance variable in your class implementation like -
@implementation myViewController
{
UITextField *activeField;
}
然后简单地实现如下方法
and then simply implement the method as below
在你的 textFieldShouldBeginEditing 中,设置 activeField
in your textFieldShouldBeginEditing, set activeField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
activeField = textField; // HERE get reference of your active field
return true;
}
为所有处理提供了一个非常好的方法
There is a very nice method provided for all handling
CGRectContainsRect
CGRectContainsRect
if (!CGRectContainsRect(viewableAreaFrame, activeTextFieldFrame))
{
/*Scroll or move view up*/
}
在您的 keyboardWillShow 方法中实现以下
Implement below in your keyboardWillShow method
前.
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float viewWidth = self.view.frame.size.width;
float viewHeight = self.view.frame.size.height;
CGRect viewableAreaFrame = CGRectMake(0.0, 0.0, viewWidth, viewHeight - keyboardHeight);
CGRect activeTextFieldFrame = [activeTextField frame];
if (!CGRectContainsRect(viewableAreaFrame, activeTextFieldFrame))
{
/*Scroll or move view up*/
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = -keyboardSize.height;
self.view.frame = f;
}];
}
}
这篇关于如何在 if 条件下对所有检查 UITextField 执行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!