1

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()
}
amsmath
  • 119
  • 7

1 Answers1

3

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for the moment. In another question here on stackoverflow I saw that it is possible to resize the NSAlert window: https://stackoverflow.com/questions/14820335/nsalert-resize-window?rq=1 So, shouldn't it be possible to set the title bar's text, too? – amsmath May 10 '19 at 20:12
  • Yeah, but that was just hackery. I mean you could try to play with the alert's window, but I don't think it's going to work. NSAlert is convenient and simple-minded and standard and Apple doesn't really want you bending it. :) – matt May 10 '19 at 20:15
  • 3
    You can set the title the same way as any other alert window property - set the alert _window_ title, e.g. `alert.window.title = whatever`. – red_menace May 10 '19 at 20:32
  • @matt Actually, I don't care about what Apple wants me to do or not to do. If I think that an icon is not enough for identifying the app that stands behind a dialog, then that's my (i.e. the programmer's) legimite point of view. I see that one should follow certain standards, but in this case I think this kind of thinking is too narrow. – amsmath May 10 '19 at 20:36
  • @red_menace Thank you. I just found that out by myself, actually. – amsmath May 10 '19 at 20:38