0
我有一個應用程序,我希望用戶在第一次打開應用程序時選擇一個位置。在他們選擇他們的位置後,我希望保存該位置,並在特定位置提供某些按鈕。目前,我已經嘗試保存用戶的位置,只有在應用程序中才能實現該位置,但是,每當應用程序關閉時舊數據不會加載。 我的兩個問題是:從頭開始保存用戶數據
- 如何保存在整個用戶的位置應用
- 建議使用哪種方法來調用基於用戶位置的不同環節? (更新): 我現在試過的是使用NSUSERDEFAULTS。這是我的代碼。
- (void)viewDidLoad {
[super viewDidLoad];
[self userLocationSave];
}
-(void)userLocationSave
{
campusLocation = self.lblUserLocation.text;
self.lblUserLocation.textColor = [UIColor whiteColor];
[self saveData];
}
-(void)saveData
{
NSString *saveLocation = self.lblUserLocation.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:saveLocation forKey:@"campusChoice"];
[defaults synchronize];
}
-(void)loadData
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadString = [defaults objectForKey:@"campusChoice"];
[self.lblUserLocation setText:loadString];
}
然後我在viewDidAppear方法中加載數據。
爲了澄清,校園位置是來自視圖控制器的用戶選擇個人數據的NSString。
用你試過的東西更新你的問題,並準確解釋不按預期工作。 – rmaddy
在'viewDidLoad'中,你應該加載你的數據,而不是保存你的數據。 – rmaddy
NScoding非常易於使用https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/index.html – DogCoffee