23
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ios アプリのテキスト入力で次のテキストボックスにサクッと移動する方法

Posted at

テキストボックスが並んでいて、 Enterされたら次のテキストボックスにフォーカスを移す方法

image

Storyboard でそれぞれの UITextField に Tagを付与。

上から順にTag 1,2,3

image

ViewControllerで

class ChangePasswordViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate {

    // ...

    // MARK: - TextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        // 今フォーカスが当たっているテキストボックスからフォーカスを外す
        textField.resignFirstResponder()
        // 次のTag番号を持っているテキストボックスがあれば、フォーカスする
        let nextTag = textField.tag + 1
        if let nextTextField = self.view.viewWithTag(nextTag) {
            nextTextField.becomeFirstResponder()
        }
        return true
    }

}
23
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?