Is there a simple input field in Cocoa?

Is there a built-in, simple, input window in Cocoa that is designed to extract a string (for example, what I remember in Visual Basic Visual Basic)?

I suppose I could create a tiny window to do this, but would prefer to use the native equivalent (if such a thing exists, I cannot find it if it exists).

Thank.

+4
source share
3 answers

If you need a dialog box with a text field, you either need to create it yourself or put it NSTextFieldinNSAlert (note that the linked answer is a modal dialog that blocks all interactions with the rest of your application, if you do not want this, you need to submit it like a sheet in a window ).

+4
source

Thanks to DarkDust for pointing me in the right direction. I would never look for “helper looks” in NSAlerts (I didn’t have the right conditions to fool Google or SO to give me a product!). I also forgot to mention that I use Swift, so I quickly typed in 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 ""
    }
}
+18
source

NSTextField. , textFld.stringValue.

-4
source

All Articles