如何检查所有检查UITextField的if条件(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)

Ex. [myTextField setDelegate:self];// you can also set the delegate in Storyboard or xib directly

然后在类实现中添加一个实例变量,如 -

@implementation myViewController { UITextField *activeField; }

然后简单地实现如下方法

在textFieldShouldBeginEditing中,设置activeField

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { activeField = textField; // HERE get reference of your active field return true; }

为所有处理提供了一种非常好的方法

CGRectContainsRect

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

在您的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; }]; } }

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

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

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

Implement below in your keyboardWillShow method

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

更多推荐