Last active
June 7, 2019 07:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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