Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/NSUserDefaults


Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/NSUserDefaults

This example is the version from Sat, 26th Jul 2013.

Project "NSUserDefaults.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "Effacer"
Const kFileQuit = "Quitter"
Const kFileQuitShortcut = ""
EventHandler Sub Open() LoadPrefs End EventHandler
End Class
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() // Load this values. dim w,h As Integer w = UserPrefs.integerForKey("winmain_width") h = UserPrefs.integerForKey("winmain_height") Label3.Text = "Window size: "+Str(w)+" x "+Str(h) End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() // Store this values. // the default values added in the "LoadPref" method // will be replaced and they won't be returned anymore. UserPrefs.setIntegerValue("winmain_width", Self.Width ) UserPrefs.setIntegerValue("winmain_height", Self.Height ) End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action() // Load a value that has no key, and no default value. // NSUserDefaults will simply return an empty string // for this key without causing an error. dim path As String = UserPrefs.stringForKey("my_file_path") if path="" then Label4.Text = "File path has not been defined" else Label4.Text = "File path: "+path end if End EventHandler
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action() dim s As String = trim( TextField1.Text ) UserPrefs.setStringValue("my_file_path", s ) End EventHandler
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control Label4 Inherits Label
ControlInstance Label4 Inherits Label
End Control
Control TextField1 Inherits TextField
ControlInstance TextField1 Inherits TextField
End Control
Control PushButton5 Inherits PushButton
ControlInstance PushButton5 Inherits PushButton
EventHandler Sub Action() Label5.Text = "Calculator view: "+UserPrefs.stringForKey("ViewDefaultsKey") End EventHandler
End Control
Control Label5 Inherits Label
ControlInstance Label5 Inherits Label
End Control
EventHandler Sub Close() SavePrefs End EventHandler
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&Fichier"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "Edition"
MenuItem EditUndo = "Annuler"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Couper"
MenuItem EditCopy = "&Copier"
MenuItem EditPaste = "Coller"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Tout Sélectionner"
End MenuBar
Module PreferenceModule
Sub LoadPrefs() // Load preferences for this app, and for this user. // File will be saved at: user/Library/Preferences/com.mycompany.appname.Plist UserPrefs = NSUserDefaultsMBS.standardUserDefaults // If needed, you can load preferences from another app (read only). // Note that it's not allowed in Sandboxed applications. UserPrefs.addSuiteNamed("com.apple.calculator") // Create a dictionnary to add some default values. // They will be used until the user save its own default values. // No need to add empty values like 0, False, "", Nil, because when // you will ask for a key/value pair, NSUserDefault will return a default // value if the key does not exist, not an error. dim dic As New Dictionary dic.Value("winmain_width")=1200 dic.Value("winmain_height")=700 dic.Value("OpenInFullSize")=True UserPrefs.registerDefaults( dic ) End Sub
Sub SavePrefs() // synchronize is periodicaly triggered by the system. // So you just need to call it when your app terminates // or when you want to make the settings available // immediatly, for example, if the user changed // something in the preference window UserPrefs.setIntegerValue("winmain_width", Window1.Width ) UserPrefs.setIntegerValue("winmain_height", Window1.Height ) UserPrefs.setBoolValue("OpenInFullSize", False ) UserPrefs.setStringValue("my_file_path", "/movies/" ) if UserPrefs.synchronize then // Prefs has been saved end if End Sub
Property UserPrefs As NSUserDefaultsMBS
End Module
End Project

The items on this page are in the following plugins: MBS MacCocoa Plugin.


The biggest plugin in space...