简体   繁体   English

如何在if条件下对所有检查UITextField进行检查

[英]How can I perform a check on all check UITextField's in if condition

我一直在寻找出解决方案,但还没有找到一个到目前为止,请参考答案这个 ,我想知道是否有一种方法,我可以对所有UITextFields执行,而不必硬编码值的检查,感谢帮助。

first your make sure that all textField have their delegate set to self ( means your viewController) 首先,请确保所有textField的委托都设置为self(表示您的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 然后只需实现以下方法

in your textFieldShouldBeginEditing, set activeField 在您的textFieldShouldBeginEditing中,设置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 CGRect包含Rect

 if (!CGRectContainsRect(viewableAreaFrame, activeTextFieldFrame))
 {
     /*Scroll or move view up*/
 } 

Implement below in your keyboardWillShow method 在您的keyboardWillShow方法中实现以下内容

EX. 例如

 - (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;
            }];
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM