Is there a simple input box in Cocoa?

Thank you DarkDust for pointing me in the right direction. I would never have searched for "accessory views" in NSAlerts (I was lacking the right terms to trick Google or SO into giving me the goods!). I also forgot to mention that I'm using Swift, so I've knocked up a quick translation:

func getString(title: String, question: String, defaultValue: String) -> String {
    let msg = NSAlert()
    msg.addButtonWithTitle("OK")      // 1st button
    msg.addButtonWithTitle("Cancel")  // 2nd button
    msg.messageText = title
    msg.informativeText = question

    let txt = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
    txt.stringValue = defaultValue

    msg.accessoryView = txt
    let response: NSModalResponse = msg.runModal()

    if (response == NSAlertFirstButtonReturn) {
        return txt.stringValue
    } else {
        return ""
    }
}

If you'd like a dialog with a text field, you either need to create it yourself or put an NSTextField into an NSAlert (note that the linked answer presents a modal dialog that will block all interaction with the rest of your application; if you don't want that, you need to present it as a sheet on a window).


Updating for Swift 5. I always put reusable items like alerts in an app manager class. And I like to keep my closures as a typealias to better organize them and keep the arguments cleaner.

typealias promptResponseClosure = (_ strResponse:String, _ bResponse:Bool) -> Void

func promptForReply(_ strMsg:String, _ strInformative:String, vc:ViewController, completion:promptResponseClosure) {

        let alert: NSAlert = NSAlert()

        alert.addButton(withTitle: "OK")      // 1st button
        alert.addButton(withTitle: "Cancel")  // 2nd button
        alert.messageText = strMsg
        alert.informativeText = strInformative

        let txt = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
        txt.stringValue = ""

        alert.accessoryView = txt
        let response: NSApplication.ModalResponse = alert.runModal()

        var bResponse = false
        if (response == NSApplication.ModalResponse.alertFirstButtonReturn) {
            bResponse = true
        }

        completion(txt.stringValue, bResponse)

    }

Then call it like so (I needed this for a git management part of my app):

myAppManager.promptForReply("Changes were added to the repo, do you want to commit them?", "If you are commiting, add your commit message below.", vc: self, completion: {(strCommitMsg:String, bResponse:Bool) in

    if bResponse {
         print(strCommitMsg)
    }
}) 

Tags:

Cocoa

Xcode