试图找到哪个文本字段是活动的 ios
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12173802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to find which text field is active ios
提问by Lost Sorcerer
I am trying to find which textfield is active for when the I move the view when the keyboard rises. I am trying to set a property in my viewcontroller from the subview of a scrollview.
当我在键盘上升时移动视图时,我试图找到哪个文本字段处于活动状态。我正在尝试从滚动视图的子视图中在我的视图控制器中设置一个属性。
This is the code I use to display the view in the scrollview
这是我用来在滚动视图中显示视图的代码
-(void)displayView:(UIViewController *)viewController{
[[viewFrame subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
[viewFrame scrollRectToVisible:CGRectMake(0, 0, 1, 1)
animated:NO];
[viewFrame addSubview: viewController.view];
_currentViewController = viewController;
}
--EDIT--
- 编辑 -
I have changed my way of thinking about this problem. Sorry for the question being ambiguous when I posted it. I was exhausted at the time and it made sense in my head.
我已经改变了我对这个问题的思考方式。很抱歉在我发布时问题模棱两可。当时我筋疲力尽,这在我的脑海中是有道理的。
A different but similar question: Is there a common subclass of both UITextArea and UITextView that will give me the origin of the firstResponder? Or will I have to check also the class of the firstResponder before I can find the origin?
一个不同但相似的问题:UITextArea 和 UITextView 是否有一个共同的子类可以给我 firstResponder 的来源?或者我是否还必须检查 firstResponder 的类才能找到原点?
回答by Xilexio
You need to search for an object that has become a first responder. First responder object is the one using the keyboard (actually, it is he one having focus for user input). To check which text field uses the keyboard, iterate over your text fields (or just over all subviews) and use the isFirstResponder
method.
您需要搜索已成为第一响应者的对象。第一响应者对象是使用键盘的对象(实际上,它是具有用户输入焦点的对象)。要检查哪个文本字段使用键盘,请遍历您的文本字段(或仅遍历所有子视图)并使用该isFirstResponder
方法。
EDIT: As requested, a sample code, assuming all text fields are a subview of the view controller's view:
编辑:根据要求,示例代码,假设所有文本字段都是视图控制器视图的子视图:
for (UIView *view in self.view.subviews) {
if (view.isFirstResponder) {
[self doSomethingCleverWithView:view];
}
}
回答by Stanislav Smida
I did an extension for this.
我为此做了一个扩展。
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
}
}
Use:
用:
if let activeTextField = UIResponder.currentFirst() as? UITextField {
// ...
}
回答by developerdude
Why dont you give all UITextfields individual Tags textfield.tag = 1
为什么不给所有 UITextfields 单独的标签 textfield.tag = 1
then you respond to the delegate DidBeginEditing. and check which textfield.tag is active?
然后您响应委托 DidBeginEditing。并检查哪个 textfield.tag 处于活动状态?
回答by okysabeni
I first used Xilexio's solution but it was slow. I ended up using tags. Here is my code and set up as an example.
我首先使用了 Xilexio 的解决方案,但速度很慢。我最终使用了标签。这是我的代码并设置为示例。
@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;
}
Let me know if you have any questions and I'll clarify. Note that the comments are for me. Also note that some code or omitted for brevity.
如果您有任何问题,请告诉我,我会澄清。请注意,评论是给我的。还要注意一些代码或为简洁起见省略了。
回答by zevij
assuming your textfields are all the same (i.e. all currency or text) and do not require any special formatting, I would suggest the following:
假设您的文本字段都相同(即所有货币或文本)并且不需要任何特殊格式,我建议如下:
First, have an optional textField variable. For example:
首先,有一个可选的 textField 变量。例如:
var currentTextField: UITextField?
Then add the following:
然后添加以下内容:
func textFieldDidBeginEditing(textField: UITextField) {
currentTextField = textField
}
Now you can do whatever you want with the 'active' text field and you do not need to track any tags unless you need some specific formatting operation.
现在您可以使用“活动”文本字段做任何您想做的事情,除非您需要一些特定的格式化操作,否则您不需要跟踪任何标签。
回答by Mohammad Abraq
In swift 3 use the function below function in your if else statements:
在 swift 3 中,在 if else 语句中使用以下函数:
if (textField.isEditing)
[iOS] [swift3]
[iOS] [swift3]
回答by MujtabaFR
Based on Xilexio's answerbut iterating over all the views to find the requested FirstResponder
View
基于Xilexio 的回答,但遍历所有视图以找到请求的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;
}
Used like this :
像这样使用:
UIView *view = [self getFirstResponderInView:self.view];
if(view != nil){
[self doSomethingCleverWithView:view];
}