Customize NSButton in NSAlert

In an OSX App, I have create ButtonSubClassan NSButton subclass to add some behaviors to the buttons.


Could I use this for the buttons of an NSAlert ?


I tried


alert.addButton(withTitle: "modified") as! ButtonSubClass


But compiler warns me "expression of type "ButtonSubClass" is unused


And effectively, this has no effect

Accepted Reply

No, you can't force NSAlert to use buttons of a different class. The simplest thing to do is probably to not use a NSAlert for this, just a normal window or sheet with similar elements to an alert.


Note that it's generally desirable to avoid changing standard UI elements that users are used to seeing (e.g. an alert) to have non-standard behavior, because it's confusing if the behavior is unpredicatable across apps.


If you do want to customize a NSAlert, you can use the accessoryView to add extra buttons and controls above the normal buttons. But ideally, you'd still want to keep the behavior of the NSAlert pretty simple.

Replies

No, you can't force NSAlert to use buttons of a different class. The simplest thing to do is probably to not use a NSAlert for this, just a normal window or sheet with similar elements to an alert.


Note that it's generally desirable to avoid changing standard UI elements that users are used to seeing (e.g. an alert) to have non-standard behavior, because it's confusing if the behavior is unpredicatable across apps.


If you do want to customize a NSAlert, you can use the accessoryView to add extra buttons and controls above the normal buttons. But ideally, you'd still want to keep the behavior of the NSAlert pretty simple.

Thanks. That's what I did in many case, building a modal dialog instead of NSAlert.


When I say customize, it was to add in some cases an explanation (tooltip or information line) and change the bodl face of the button when mouse hovers over it.