1

I'm trying to create a window where the user can enter the activation key for the product and I've done so by creating 5 different NSTextField's, as shown in the image.

Activation Code Field

What I want to add to this is the ability for the cursor to move to the next text field once a character count has been reached (which is 5 characters maximum per textfield).

I did find code for this but it was for IOS and didn't work since i didn't know what changes to make (Code for the IOS version)

here's the code I tried

    override func viewDidLoad() {
    super.viewDidLoad()
    ActKeyOne.textDidChange(Notification.Name.init(rawValue: "textchanged"))
}

it gave the error:

Cannot convert value of type 'Notification.Name' (aka 'NSNotification.Name') to expected argument type 'Notification'
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Dhanush
  • 53
  • 6
  • Use the text field notification to count the number of characters in each text field. – El Tomato Jul 30 '20 at 08:10
  • 2
    [`textDidChangeNotification`](https://developer.apple.com/documentation/appkit/nscontrol/1428954-textdidchangenotification) & [`makeFirstResponder(_:)`](https://developer.apple.com/documentation/appkit/nswindow/1419366-makefirstresponder). Try it and if it wont work for you, edit your question with code that doesn't work for you. – zrzka Jul 30 '20 at 09:37
  • 1
    Instead of `textDidChangeNotification` you can use a delegate and [controlTextDidChange(_:)](https://developer.apple.com/documentation/appkit/nscontroltexteditingdelegate/3005177-controltextdidchange). – Willeke Jul 30 '20 at 11:45
  • Looks like controlTextDidChange( :) worked better thanks guys – Dhanush Aug 13 '20 at 13:00

1 Answers1

0

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.

Dhanush
  • 53
  • 6