簡體   English   中英

如何在if條件下對所有檢查UITextField進行檢查

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

我一直在尋找出解決方案,但還沒有找到一個到目前為止,請參考答案這個 ,我想知道是否有一種方法,我可以對所有UITextFields執行,而不必硬編碼值的檢查,感謝幫助。

首先,請確保所有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;
}

提供了一個非常好的方法來處理所有問題

CGRect包含Rect

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

在您的keyboardWillShow方法中實現以下內容

例如

 - (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