RMDan RMDan - 10 months ago 49
iOS Question

Trying to find which text field is active ios

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)

[viewFrame addSubview: viewController.view];

_currentViewController = viewController;


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?

Answer Source

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.

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];