user1822824
user1822824

Reputation: 2498

NSAlert beginSheetModalForWindow Not Showing Alert

I have a settings view controller that is presented as a sheet. It has a button that saves the settings if they are valid. If the settings are valid the view controller is dismissed. If they are not valid the user gets an alert saying the settings are not valid. My code is as follows:

var settingsValidated = false

@IBAction func dismissSettings(sender: AnyObject) {

    if settingsValidated == true {

        dismissViewController(self)

    } else {

        let alert = NSAlert()
        alert.messageText = "Warning"
        alert.addButtonWithTitle("OK")
        alert.informativeText = "Your settings did not validate!"

        let window = NSApplication.sharedApplication().mainWindow
        let res = alert.beginSheetModalForWindow(window!, completionHandler: nil)

    }
}

If settingsValidated is set to true everything works as expected but when I set settingsValidated to false nothing happens. The alert never shows. What am I missing? I do not receive any errors in Xcode.

Please note this question is about OS X NOT iOS.

Upvotes: 0

Views: 841

Answers (1)

l'L'l
l'L'l

Reputation: 47284

It's not showing up because you aren't doing anything with the res object! — so remove it:

        alert.beginSheetModalForWindow(window!, completionHandler: nil)

NSAlert Class Reference

Upvotes: -2

Related Questions