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

微笑、不失礼 提交于 2019-12-12 01:44:09

问题


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.


回答1:


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

    }


来源:https://stackoverflow.com/questions/35738921/how-can-i-perform-a-check-on-all-check-uitextfields-in-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!