试图find哪个文本字段是活跃的ios

当我在键盘上升时移动视图时,我正在尝试查找哪个文本字段处于活动状态。 我想从scrollview的子视图在我的viewcontroller中设置一个属性。

这是我用来在滚动视图中显示视图的代码

-(void)displayView:(UIViewController *)viewController{ [[viewFrame subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; [viewFrame scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; [viewFrame addSubview: viewController.view]; _currentViewController = viewController; } 

– 编辑 –

我改变了我对这个问题的思考方式。 对不起,当我发布它的问题是模糊的。 当时我筋疲力尽,头脑里有道理。

一个不同但相似的问题:是否有一个UITextArea和UITextView的共同的子类,将给我firstResponder的起源? 或者我还需要检查firstResponder的类,才能find原点?

您需要search已成为第一响应者的对象。 第一响应者对象是使用键盘的对象(实际上,他是一个有用户input焦点的对象)。 要检查哪个文本字段使用键盘,请遍历文本字段(或者遍历所有子视图)并使用isFirstResponder方法。

编辑:根据要求,示例代码,假设所有文本字段是视图控制器视图的子视图:

 for (UIView *view in self.view.subviews) { if (view.isFirstResponder) { [self doSomethingCleverWithView:view]; } } 

我为此做了扩展。

 public extension UIResponder { private struct Static { static weak var responder: UIResponder? } public static func currentFirst() -> UIResponder? { Static.responder = nil UIApplication.shared.sendAction(#selector(UIResponder._trap), to: nil, from: nil, for: nil) return Static.responder } @objc private func _trap() { Static.responder = self } } 

使用:

 if let activeTextField = UIResponder.currentFirst() as? UITextField { // ... } 

假设你的textfields都是一样的(即所有的货币或文本),并不需要任何特殊的格式,我会build议如下:

首先,有一个可选的textFieldvariables。 例如:

 var currentTextField: UITextField? 

然后添加以下内容:

 func textFieldDidBeginEditing(textField: UITextField) { currentTextField = textField } 

现在,您可以使用“活动”文本字段进行任何操作,除非需要特定的格式化操作,否则不需要跟踪任何标签。

为什么不给所有的UITextfields个人标签textfield.tag = 1

那么你回应委托DidBeginEditing。 并检查哪个textfield.tag是活动的?

在swift中,在你的if语句中使用下面的函数:

  if (textField.isEditing) 

[iOS] [swift3]

我第一次使用Xilexio的解决scheme,但速度很慢。 我结束了使用标签。 这是我的代码,并作为一个例子。

 @property (nonatomic) NSInteger currentFormField; typedef NS_ENUM(NSInteger, IOUFormField) { IOUFormFieldName, IOUFormFieldAmount, IOUFormFieldDescription, IOUFormFieldDate }; 

 self.nameField.tag = IOUFormFieldName; self.amountField.tag = IOUFormFieldAmount; self.descriptionField.tag = IOUFormFieldDescription; self.dateField.tag = IOUFormFieldDate; -(void)keyboardWillShow:(NSNotification *)notification { // Move the scroll view to a position where the user can see the top and bottom form fields // For example, if the user is on the description field, they should be able to see the date field and the amount field. // The keyboard rect value comes as a NSValue * (a wrapped NSRect) with origin and size. // The origin is using screen coordinates which is pixel based so don't use it. // Use the size. Seems like it is density based. CGFloat viewableScreenHeight = self.view.frame.size.height - keyboardFrameBeginRect.size.height; // When the user is on a form field, get the current form field y position to where the scroll view should move to CGFloat currentFormFieldYPosition = 0; switch (self.currentFormField) { case IOUFormFieldName: { currentFormFieldYPosition = self.nameField.frame.origin.y; // If the scroll view is at the bottom and the user taps on the name field, move the scroll view to the top. // This is so that users can see the give/get segments. [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES]; break; } case IOUFormFieldAmount: { currentFormFieldYPosition = self.amountField.frame.origin.y; break; } case IOUFormFieldDescription: { currentFormFieldYPosition = self.descriptionField.frame.origin.y; break; } case IOUFormFieldDate: { currentFormFieldYPosition = self.dateField.frame.origin.y; break; } default: break; } // I want the current form field y position to be 100dp from the keyboard y position. // 50dp for the current form field to be visible and another 50dp for the next form field so users can see it. CGFloat leftoverTopHeight = viewableScreenHeight - 100; // If the current form field y position is greater than the left over top height, that means that the current form field is hidden // We make the calculations and then move the scroll view to the right position if (currentFormFieldYPosition > leftoverTopHeight) { CGFloat movedScreenPosition = currentFormFieldYPosition - leftoverTopHeight; [self.scrollView setContentOffset:CGPointMake(0, movedScreenPosition) animated:YES]; } } #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { switch (textField.tag) { case IOUFormFieldName: self.currentFormField = IOUFormFieldName; break; case IOUFormFieldAmount: self.currentFormField = IOUFormFieldAmount; break; case IOUFormFieldDescription: self.currentFormField = IOUFormFieldDescription; break; case IOUFormFieldDate: self.currentFormField = IOUFormFieldDate; default: break; } return true; } 

如果您有任何问题,请告诉我,我会澄清。 请注意,评论是给我的。 另外请注意,一些代码或为简洁省略。

基于Xilexio的答案,但迭代所有的意见,以find所需的FirstResponder视图

 -(UIView*)getFirstResponderInView:(UIView*)parentView{ UIView* requestedView = nil; for (UIView *view in parentView.subviews) { if (view.isFirstResponder) { [view resignFirstResponder]; } else if (view.subviews.count > 0) { requestedView = [self getFirstResponderInView:view]; } if (requestedView != nil) { return requestedView; } } return nil; } 

像这样使用:

 UIView *view = [self getFirstResponderInView:self.view]; if(view != nil){ [self doSomethingCleverWithView:view]; }