Как перемещаться по текстовым полям (кнопки «Далее» / «Готово»)

Я согласен с Cletus. Используйте QuickSort , затем удалите дубликаты

476
задан picciano 1 May 2018 в 14:58
поделиться

3 ответа

В Какао для Mac OS X у вас есть следующая цепочка респондентов, где вы можете спросить текстовое поле, какой элемент управления должен иметь фокус следующим. Это то, что делает работу табуляции между текстовыми полями. Но поскольку устройства iOS не имеют клавиатуры, только сенсорную клавиатуру, эта концепция не пережила переход на Cocoa Touch.

В любом случае это можно легко сделать с двумя предположениями:

  1. Все "tabbable" UITextField находятся в одном родительском представлении.
  2. Их "порядок табуляции" определяется свойством tag.

Предполагая, что вы можете переопределить textFieldShouldReturn: как это:

-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
  NSInteger nextTag = textField.tag + 1;
  // Try to find next responder
  UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
  if (nextResponder) {
    // Found next responder, so set it.
    [nextResponder becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
    [textField resignFirstResponder];
  }
  return NO; // We do not want UITextField to insert line-breaks.
}

Добавьте еще код и предположения также можно игнорировать.

Swift 4.0

 func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    let nextTag = textField.tag + 1
    // Try to find next responder
    let nextResponder = textField.superview?.viewWithTag(nextTag) as UIResponder!

    if nextResponder != nil {
        // Found next responder, so set it
        nextResponder?.becomeFirstResponder()
    } else {
        // Not found, so remove keyboard
        textField.resignFirstResponder()
    }

    return false
}

Если супервизор текстового поля будет UITableViewCell, то следующим ответчиком будет

let nextResponder = textField.superview?.superview?.superview?.viewWithTag(nextTag) as UIResponder!
573
ответ дан 22 November 2019 в 22:47
поделиться

After you exit from one text field, you call [otherTextField becomeFirstResponder] and the next field gets focus.

This can actually be a tricky problem to deal with since often you'll also want to scroll the screen or otherwise adjust the position of the text field so it's easy to see when editing. Just make sure to do a lot of testing with coming into and out of the text fields in different ways and also leaving early (always give the user an option to dismiss the keyboard instead of going to the next field, usually with "Done" in the nav bar)

9
ответ дан 22 November 2019 в 22:47
поделиться

in textFieldShouldReturn you should check that the textfield you are currently on is not the last one when they click next and if its n ot dont dismiss the keyboard..

2
ответ дан 22 November 2019 в 22:47
поделиться