gpt4 book ai didi

ios - 显示键盘时获取当前文本字段

转载 作者:行者123 更新时间:2023-12-01 16:16:08 25 4
gpt4 key购买 nike

我想在显示键盘时获取正在编辑的当前文本字段。我需要这个来确定帧动画的一些 y 值。但是,我的 keyboardWasShown 方法正在 Controller 中运行,因此我可以轻松获取 View ,但不确定如何获取正确的文本字段。我在这个 View 上有两个文本字段。

//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 的代表并实现您的

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

创建一个 UITextField 变量 UITextField *flagTextField;
- (void)textFieldDidBeginEditing:(UITextField *)textField{
flagTextField = textField;
}

现在你有了 textfield 的实例

关于ios - 显示键盘时获取当前文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25390653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com