簡體   English   中英

顯示鍵盤時獲取當前文本字段

[英]Get current text field when keyboard was shown

我想顯示鍵盤時正在編輯的當前文本字段。 我需要它來確定幀動畫的一些y值。 但是,我的keyboardWasShown方法正在控制器中運行,因此我可以輕松獲取視圖,但不確定如何獲取正確的文本字段。 我在此視圖上有兩個文本字段。

//Move view to match keyboard when shown
-(void)keyboardWasShown:(NSNotification*)aNotification{

    //Get frame of keyboard
    NSValue* keyboardEndFrameValue = [[aNotification userInfo] objectForKey: UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardEndFrame = [keyboardEndFrameValue CGRectValue];

    //Get animation properties of keyboard
    NSNumber* animationDurationNumber = [[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration = [animationDurationNumber intValue];
    NSNumber* animationCurveNumber = [[aNotification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    UIViewAnimationCurve animationCurve = [animationCurveNumber intValue];
    UIViewAnimationOptions animationOptions = animationCurve << 16;

    //Set up animation
    [UIView animateWithDuration:animationDuration delay:0.0 options:animationOptions animations:^{
            CGRect viewFrame = self.view.frame;
            viewFrame.origin.y -= keyboardEndFrame.origin.y;
//I need the access to the text field here to determine y value.
            self.view.frame = viewFrame;
     } completion:^(BOOL finished){}];
}

使用UITextField的Delegates並實現您的

- (void)textFieldDidBeginEditing:(UITextField *)textField;

創建一個UITextField變量UITextField *flagTextField;

- (void)textFieldDidBeginEditing:(UITextField *)textField{
     flagTextField = textField;
}

現在您有了textfield的實例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM