0
votes

I'm running into a few issues trying to set up a Preferences menu for my app. I'm currently using Swift and Xcode 7.

I have a Preferences window that opens from the Main menu. However, once it is open is where my issues lie. I have 2 buttons, one that resets the user inputs, and the other which is supposed to save the information.

I've got the reset button set up to make the text inputs default values.

My next goal is to have the save button set the variables with any entered text in the text fields and close the Preferences window. Then, in an ideal world, I'd like to be able to re-open the Preferences to make sure they saved.

Is this possible?

Currently, my program hangs if I click the Save button, or the Preferences window will not reopen if the red X in the corner is clicked.

Here is my current Preferences Window code, not even sure what of this is unnecessary:

import Cocoa
protocol PreferencesWindowDelegate {
func preferencesDidUpdate()
}
class PreferencesWindow: NSWindowController, NSWindowDelegate {
    @IBAction func ResetPref(sender: NSButton) {
    UserName.stringValue = "Name"
    UserEmail.stringValue = "Email"
    ManEmail.stringValue = "Man. Email"
    SSEmail.stringValue = "SS Email"
}
@IBOutlet var SSEmail: NSTextField!
@IBOutlet var ManEmail: NSTextField!
@IBOutlet var UserEmail: NSTextField!
@IBOutlet var UserName: NSTextField!
@IBAction func SavePref(sender: NSButton) {
    }
override var windowNibName : String! {
    return "PreferencesWindow"
        }
    override func controlTextDidBeginEditing(_: NSNotification){
    func controlTextDidChange(obj: NSNotification){  
        }
func windowDidLoad() {
    super.windowDidLoad()
    self.window?.center()
    self.window?.makeKeyAndOrderFront(nil);
    NSApp.activateIgnoringOtherApps(true);
}
var delegate: PreferencesWindowDelegate?       
        func controlTextDidEndEditing(obj: NSNotification){}
func windowWillClose(notification: NSNotification) {
    delegate?.preferencesDidUpdate()
    }
}
}

Any guidance is appreciated, as this is my first time working in Swift!

1

1 Answers

0
votes

Is your preferencesDidUpdate() function defined?

You typically save preferences by using NSUserDefaults, which saves preferences to a .plist file. This is the Cocoa way. To save NSUSerDefaults, you just do

let defaults = NSUserDefaults.standardUserDefaults()

    defaults.setObject(myValue, forKey: myKey)