Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple input box in Cocoa?

Tags:

xcode

cocoa

Is there a built-in, simple, input box in Cocoa which is designed for retrieving a string (like there is in good ol' Visual Basic that I remember)?

I suppose I could design a tiny window to do that but would prefer to use a native equivalent (if such a thing exists; I can't find it if it does).

Thanks.

like image 988
Marc Fearby Avatar asked Feb 06 '15 09:02

Marc Fearby


People also ask

What is a UITextField?

An object that displays an editable text area in your interface.

What is a text field on Iphone?

A text field is a UI element that enables the app to get user input.


3 Answers

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 ""
    }
}
like image 84
Marc Fearby Avatar answered Oct 05 '22 05:10

Marc Fearby


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).

like image 35
DarkDust Avatar answered Oct 05 '22 05:10

DarkDust


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)
    }
}) 

like image 36
PruitIgoe Avatar answered Oct 05 '22 03:10

PruitIgoe