How to capture textfield input from NSTextField

Hello:


I want to capture the input value in a text field into a variable. Here is my code:


var window: NSWindow?
   
    func checkGroupName() {
        let alert = NSAlert()
        alert.messageText = "Please enter your group name"
        alert.addButton(withTitle: "Save")
        alert.addButton(withTitle: "Cancel")

        let inputTextField = NSTextField(frame: NSRect(x: 0, y: 0, width: 300, height: 24))
        inputTextField.placeholderString = ("Enter your group name")
        alert.accessoryView = inputTextField
        alert.runModal()
        let object = NSTextField()
     let value = object.stringValue
     print("The value of Group Name is \(value)")

}


What should I add to the above code to get the value entered in a variable i can use later in the code? "value" above is empty.

Replies

SOLVED


with the following line added:

               let enteredString = inputTextField.stringValue
                print("Entered Group Name is = \"\(enteredString)\"")

What was the purpose of:

        let object = NSTextField()
        let value = object.stringValue

When you do this, you create a new textField, empty.

So of course stringValue has nothing in it.


So, you should remove those 2 lines as well.


Good you found yourself, don't forget to close the thread on your answer.

BTW, that's more a getting started than a Cocoa question.