How to create and get return Value from Cocoa Dialog?

前端 未结 4 1113
抹茶落季
抹茶落季 2020-12-13 15:46

In my application, I want to create a dialog box with one text field, and a button, through which I can prompt user and get back user entered value.

How do I do this

4条回答
  •  忘掉有多难
    2020-12-13 16:47

    An example in Swift as of Xcode 7.2.1 and OS X 10.11:

    let a = NSAlert()
    a.messageText = "Please enter a value"
    a.addButtonWithTitle("Save")
    a.addButtonWithTitle("Cancel")
    
    let inputTextField = NSTextField(frame: NSRect(x: 0, y: 0, width: 300, height: 24))
    inputTextField.placeholderString = "Enter string"
    a.accessoryView = inputTextField
    
    a.beginSheetModalForWindow(self.window!, completionHandler: { (modalResponse) -> Void in
        if modalResponse == NSAlertFirstButtonReturn {
            let enteredString = inputTextField.stringValue
            print("Entered string = \"\(enteredString)\"")
        }
    })
    

提交回复
热议问题