标签:
1.设置捆绑包
分组
文本框设置
安全文本框设置
多值字段
拨动开关设置
滑动条设置
子设置视图(more)
2.读取应用中得设置
1 - (void)viewDidAppear:(BOOL)animated 2 { 3 [super viewDidAppear:animated]; 4 [self refreshFields]; 5 } 6 - (void)refreshFields 7 { 8 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 9 self.officerLabel.text = [defaults objectForKey:kOfficerKey]; 10 self.authorizationCodeLabel.text = [defaults objectForKey:kAuthorizationCodeKey]; 11 self.rankLabel.text = [defaults objectForKey:kRankKey]; 12 self.warpDriveLabel.text = [defaults boolForKey:kWarpDriverKey] ? @"Engaged" : @"Disabled"; 13 self.warpFactorLabel.text = [[defaults objectForKey:kWarpFactorKey] stringValue]; 14 self.favoriteTeaLabel.text = [defaults objectForKey:kFavoriteTeaKey]; 15 self.favoriteCaptainLabel.text = [defaults objectForKey:kFavoriteCaptainKey]; 16 self.favoriteGadgetLabel.text = [defaults objectForKey:kFavoriteGadgetKey]; 17 self.favoriteAlienLabel.text = [defaults objectForKey:kFavoriteAlienKey]; 18 } 19 20 - (void)flipsideViewControllerDidFinish:(FMFlipsideViewController *)controller 21 { 22 //返回翻转时,加载一次. 23 [self refreshFields]; 24 [self dismissViewControllerAnimated:YES completion:nil]; 25 }
3.应用中修改默认设置
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 [self refreshFields]; 5 } 6 - (void)refreshFields 7 { 8 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 9 self.engineSwitch.on = [defaults boolForKey:kWarpDriverKey]; 10 self.warpFactorSlider.value = [defaults floatForKey:kWarpFactorKey]; 11 } 12 - (IBAction)engineSwitchTapped 13 { 14 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 15 [defaults setBool:self.engineSwitch.on forKey:kWarpDriverKey]; 16 [defaults synchronize]; 17 } 18 - (IBAction)warpSliderTouched 19 { 20 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 21 [defaults setFloat:self.warpFactorSlider.value forKey:kWarpFactorKey]; 22 [defaults synchronize]; 23 }
4.注册默认值(应用不知道设置捆绑包中得默认值)
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 2 { 3 NSDictionary * defaults = @{kWarpDriverKey : @YES, 4 kWarpFactorKey : @5, 5 kFavoriteAlienKey : @"Vulcan"}; 6 [[NSUserDefaults standardUserDefaults] registerDefaults:defaults]; 7 8 return YES; 9 }
5.保证设置有效
1 - (void)viewWillAppear:(BOOL)animated 2 { 3 [super viewWillAppear:animated]; 4 UIApplication * app = [UIApplication sharedApplication]; 5 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:app]; 6 } 7 - (void)viewDidDisappear:(BOOL)animated 8 { 9 [super viewDidDisappear:animated]; 10 [[NSNotificationCenter defaultCenter] removeObserver:self]; 11 } 12 - (void)applicationWillEnterForeground:(NSNotification *)notification 13 { 14 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 15 [defaults synchronize]; 16 [self refreshFields]; 17 }
标签:
原文地址:http://www.cnblogs.com/fengmin/p/4664644.html