0

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.

user1822824
  • 2,478
  • 6
  • 41
  • 65

1 Answers1

-2

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

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • @user1822824: "it still does not work" tells me nothing other than *you* are clearly doing something wrong. i removed `let res =` from the last line and it works perfectly fine for me. – l'L'l Apr 03 '16 at 22:15
  • I don't receive any error in Xcode but the alert doesn't show when the button is clicked. I was finally able to get it by making a number of modifications to my NSAlert code. I will post my modified code as an answer. – user1822824 Apr 03 '16 at 23:19
  • Of course there's no error, because the code runs as expected. **You have an object that does nothing (as I've already mentioned), and It's quite obvious in the last line of your code!** – l'L'l Apr 04 '16 at 02:49