Skip to content

Instantly share code, notes, and snippets.

@ssebi
Last active June 7, 2019 07:45
Show Gist options
  • Save ssebi/a759c9bf1074958ad2a1470d7581e936 to your computer and use it in GitHub Desktop.
Save ssebi/a759c9bf1074958ad2a1470d7581e936 to your computer and use it in GitHub Desktop.
Extension to connect more UITextFields together and enable "Next" keyboard button to cycle through them. When reaching the last one button becomes "Done" and keyboard is dismissed
//
// UITextField.swift
// MasjidLink
//
// Created by Sebastian Vidrea on 07/06/2019.
// Copyright © 2019 Sebastian Vidrea. All rights reserved.
//
import Foundation
import UIKit
extension UITextField {
class func connectFields(fields: [UITextField]) -> Void {
guard let last = fields.last else {
return
}
for index in 0 ..< fields.count - 1 {
fields[index].returnKeyType = .next
fields[index].addTarget(fields[index + 1],
action: #selector(UIResponder.becomeFirstResponder),
for: .editingDidEndOnExit)
}
last.returnKeyType = .done
last.addTarget(last, action: #selector(UIResponder.resignFirstResponder),
for: .editingDidEndOnExit)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment