4

I have several UITextfields, I am trying to set the first responder to be the first UITextField which its tag should equal 0.

This is how I am trying to achieve this in code

[[self.cutField viewWithTag:0] becomeFirstResponder];

But the problem being is that this UITextField never becomes the first responder.

halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

5 Answers5

3

[self.cutField becomeFirstResponder] should work

You don't need the subviews as the textfield is a UIView.

You may use it's parent view, such as in the controller class,

-(void) viewDidLoad {
 [[self.view viewWithTag:0]  becomeFirstResponder];
}

Does this make sense?

1

It's working fine for me, in the textFieldShouldReturn delegate method you can use the textField object specified in the delegate method with the tag of your textField which you want to resign.You can also use the object of the textField you want to resign([cutField resignFirstResponder]).

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [[textField viewWithTag:0] resignFirstResponder];
    return YES;
}

if you are not using IB,you have to set tags to each of your textFields programatically.

abhi
  • 620
  • 5
  • 14
  • This code works because `viewWithTag:` begins its search with the receiver. All views have a default tag of 0. So calling `[someView viewWithTag:0] will return `someView` as long as `someView` doesn't have some other specific tag. This is pointless. – rmaddy Nov 14 '13 at 03:50
  • But why would you call `viewWithTag:` here? Why not just do `[textField resignFirstResponder];`? The `textField` isn't going to have any text field subviews. – rmaddy Nov 14 '13 at 04:00
  • 1
    No. Calling `resignFirstResponder` will only resign the text field you call it on. Besides, only one text field can be the first responder anyway. There is NO need to use a tag in the code you posted in your answer. – rmaddy Nov 14 '13 at 04:08
  • hey maddy i know that,thats what i said in the above comment.But i answered to the question he wants to use the tag. – abhi Nov 14 '13 at 04:14
  • Using this code when the method is called textField is comming back as null when I log it. – HurkNburkS Nov 14 '13 at 21:34
1

-viewWithTag: method returns UIView object so you need to cast its type explicitly:

    UITextField *yourTextField = (UITextField *)
    [self.view viewWithTag:0];

 // now set to first responder
  [yourTextField becomeFirstResponder];
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
1

use this:

[(UITextField*)self.cutField viewWithTag:0] becomeFirstResponder]
Hoven
  • 563
  • 1
  • 5
  • 24
0
@interface TextInputVC ()

@property (nonatomic, strong) UITextField    *inputTextView;

@end

- (void)viewDidLoad {
    self.inputTextView                   = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 100)];
    self.inputTextView.backgroundColor   = [UIColor clearColor];
    self.inputTextView.delegate          = self;
    self.inputTextView.tag               = 1000;
    self.inputTextView.text              = self.fieldTextValue;
    [self.view addSubview:self.inputTextView];

    // [inputTextView becomeFirstResponder];
    [self showKeyboard];
}

-(void) showKeyboard {
    UITextField *textfield  = (UITextField *)[self viewWithTag:1000];
    [textfield becomeFirstResponder];
}
Bishal Ghimire
  • 2,580
  • 22
  • 37