How to make UITextField in UITableView become first responder

No comments on How to make UITextField in UITableView become first responder

Written by

in

I loadView create a UITextField ivar.

- (void)loadView
{
    [super loadView];

    _userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 10, 280, 30)];
    _userNameTextField.placeholder = @"Введите имя пользователя";
    [_userNameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
    _userNameTextField.returnKeyType = UIReturnKeyGo;
    _userNameTextField.delegate = self;
    _userNameTextField.tag = 1;
...

In ViewDidAppear make it first responder:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [_userNameTextField becomeFirstResponder];
}

In cellForRow add it as a subview:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [cell addSubview:_userNameTextField];

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *