swiftcocoansalert

How do I set the text in the title bar of an NSAlert modal dialog


I am starting with Swift and Cocoa under MacOS and I am writing a simple Hello World app with an NSAlert modal dialog. Now, it seems standard that these dialog boxes do not have a title in the title bar, which I find problematic because the user should be aware of which app this dialog belongs to. So, I would like to display the app's title in this title bar and I do not see any method within the NSAlert class which takes care of that. Can anybody help?

This is my Alert function that I am calling when a button is pressed.

func ShowMessage(question: String, text: String) -> Void
{
    let alert: NSAlert = NSAlert()
    alert.messageText = question
    alert.informativeText = text
    alert.runModal()
}

Solution

  • The alert has your app's icon, so there's no difficulty about knowing what app this is. And you can always use the app name in the text if you like. There's nothing else you can do, really, if you're going to use NSAlert. Your other option is to make your own window and run it modally; see Creating a fully customized NSAlert for example.