RegisterDefaults should be called unconditionally before querying NSUserDefaults. Otherwise if you happen to set the first name in settings then none of the other defaults will work unless the user happened to set them all.
This commit is contained in:
Adam Kemp 2016-01-10 15:01:32 -06:00
Родитель bed745dea5
Коммит 06763a5e35
3 изменённых файлов: 3 добавлений и 11 удалений

Просмотреть файл

@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation; using Foundation;
using UIKit; using UIKit;
@ -12,7 +9,7 @@ namespace AppPrefs
/// application events from iOS. /// application events from iOS.
/// </summary> /// </summary>
[Register ("AppDelegate")] [Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate public class AppDelegate : UIApplicationDelegate
{ {
// class-level declarations // class-level declarations
NSObject observer; NSObject observer;
@ -21,6 +18,7 @@ namespace AppPrefs
public override void FinishedLaunching (UIApplication application) public override void FinishedLaunching (UIApplication application)
{ {
Settings.LoadDefaultValues();
observer = NSNotificationCenter.DefaultCenter.AddObserver ((NSString)"NSUserDefaultsDidChangeNotification", DefaultsChanged); observer = NSNotificationCenter.DefaultCenter.AddObserver ((NSString)"NSUserDefaultsDidChangeNotification", DefaultsChanged);
DefaultsChanged (null); DefaultsChanged (null);
} }

Просмотреть файл

@ -1,5 +1,4 @@
using UIKit; using UIKit;
using CoreGraphics;
using System; using System;
using Foundation; using Foundation;

Просмотреть файл

@ -1,4 +1,3 @@
using System;
using Foundation; using Foundation;
namespace AppPrefs namespace AppPrefs
@ -31,7 +30,7 @@ namespace AppPrefs
const string nameColorKey = "nameColorKey"; const string nameColorKey = "nameColorKey";
const string backgroundColorKey = "backgroundColorKey"; const string backgroundColorKey = "backgroundColorKey";
static void LoadDefaultValues () public static void LoadDefaultValues ()
{ {
var settingsDict = new NSDictionary (NSBundle.MainBundle.PathForResource ("Settings.bundle/Root.plist", null)); var settingsDict = new NSDictionary (NSBundle.MainBundle.PathForResource ("Settings.bundle/Root.plist", null));
@ -61,14 +60,10 @@ namespace AppPrefs
var appDefaults = new NSDictionary (firstNameKey, FirstName, lastNameKey, LastName, nameColorKey, (int)TextColor, backgroundColorKey, (int)BackgroundColor); var appDefaults = new NSDictionary (firstNameKey, FirstName, lastNameKey, LastName, nameColorKey, (int)TextColor, backgroundColorKey, (int)BackgroundColor);
NSUserDefaults.StandardUserDefaults.RegisterDefaults (appDefaults); NSUserDefaults.StandardUserDefaults.RegisterDefaults (appDefaults);
NSUserDefaults.StandardUserDefaults.Synchronize ();
} }
public static void SetupByPreferences () public static void SetupByPreferences ()
{ {
var testValue = NSUserDefaults.StandardUserDefaults.StringForKey (firstNameKey);
if (testValue == null)
LoadDefaultValues ();
FirstName = NSUserDefaults.StandardUserDefaults.StringForKey (firstNameKey); FirstName = NSUserDefaults.StandardUserDefaults.StringForKey (firstNameKey);
LastName = NSUserDefaults.StandardUserDefaults.StringForKey (lastNameKey); LastName = NSUserDefaults.StandardUserDefaults.StringForKey (lastNameKey);
TextColor = (TextColors)(int)NSUserDefaults.StandardUserDefaults.IntForKey (nameColorKey); TextColor = (TextColors)(int)NSUserDefaults.StandardUserDefaults.IntForKey (nameColorKey);