The answer to part of this this question was posted here by @cheesey
here is the complete code to create a window that takes the license key for a product from a user (This is using swift 4)
.
First set the Text Fields as the delegates and first responders in the viewDidLoad
Function and then change the first responder once the string limit is hit
class CommercialActivationView: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
@IBOutlet weak var firsttextfield: NSTextField!
@IBOutlet weak var secondtextfield: NSTextField!
@IBOutlet weak var thirdtextfield: NSTextField!
firsttextfield.window?.makeFirstResponder(firsttextfield)
firsttextfield.delegate = self
}
func makeFirstResponder() {
if firsttextfield.stringValue.count == 5 {
firsttextfield.window?.makeFirstResponder(secondtextfield)
}
if secondtextfield.stringValue.count == 5 {
secondtextfield.window?.makeFirstResponder(thirdtextfield)
}
}
}
Now to create the extension that creates the character limit or the text field every time the user edits the TextField
(Here i'm limiting the number of characters per text field to 5)
.
extension CommercialActivationView: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
let object = obj.object as! NSTextField
if object.stringValue.count > 5{
object.stringValue = String(object.stringValue.dropLast())
makeFirstResponder()
}
}
This works such that once 5 characters are reached in 1 TextField
it switches to the next one automatically. Also the code I've posted is for 3 TextFields
more text fields can be add if needed.