我正在構建一個點擊計數應用程序(例如:進入房間的人)。主要目標是能夠擁有多個櫃檯(例如:主廳櫃檯,白色櫃檯櫃檯,綠色房間櫃檯等)。NSUserDefaults奇怪的行爲 - 更改存儲的值
該應用程序包括:
1)計數器的列表 - CounterListTableViewController
,具有功能列表
2)創建新的項目(計數器)計數器頁 - RowCounterViewController
問題#1:在RowCounterViewController
計數值被存儲到NSUserDefaults
。敲擊後,讓我們說3次在計數器中,通過導航欄按鈕「返回」返回列表,然後返回到該計數器在UILabel中顯示3。到目前爲止,一切都很好,但如果我回到列表超過1次,數值開始改變。發生什麼事?
這是對形勢的NSLog上面解釋:
2015-01-02 23:51:56.243 RowCounter[8522:380477] {
0 = 0;
1 = 0;
2 = 0;
}
// here I tap 3 times in the counter with index "0" in the list
2015-01-02 23:52:07.184 RowCounter[8522:380477] {
0 = 3;
1 = 0;
2 = 0;
}
// I return to the list and go back to the same counter, 3 times is there
2015-01-02 23:52:11.594 RowCounter[8522:380477] {
0 = 0;
1 = 0;
2 = 0;
}
// I return again to the list and again back to the same counter, it now shows 0 (but should be 3)
2015-01-02 23:52:17.902 RowCounter[8522:380477] {
0 = 0;
1 = 0;
2 = 0;
}
// I now go to the counter with index "1" and tap 5 times
2015-01-02 23:52:27.591 RowCounter[8522:380477] {
0 = 0;
1 = 5;
2 = 0;
}
// I return to the list and go to a different counter with index "2", in the counter with index "1" count is 5
2015-01-02 23:52:30.401 RowCounter[8522:380477] {
0 = 0;
1 = 5;
2 = 0;
}
// I again return to the list and again go to a different counter with index "2", in the counter with index "1" count is still 5
2015-01-02 23:52:34.029 RowCounter[8522:380477] {
0 = 0;
1 = 5;
2 = 0;
}
// I again return to the list and again go to a different counter with index "2", in the counter with index "1" count is still 5 - So if not returning to the counter with count >0 it holds that count
2015-01-02 23:52:37.144 RowCounter[8522:380477] {
0 = 0;
1 = 0;
2 = 0;
}
// I return to the list and go to the counter with index "1" and count 5 clears.
問題2:這可能是連接到第一個問題,我失去了計數時,Home鍵被按下,但因爲我失去無論如何,如果第一個問題得到解決,這個問題就可以解決。
代碼,我存儲和加載視圖:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCount) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCount) name:UIApplicationWillTerminateNotification object:nil];
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"] ;
NSLog(@"%@", dict);
NSString *key = [NSString stringWithFormat:@"%ld", (unsigned long)_itemIndex] ;
NSInteger numCounter = [[dict objectForKey:key] intValue] ;
if (numCounter) {
// counter = [numCounter intValue] ;
count.text = [NSString stringWithFormat:@"%ld", (long)numCounter] ;
} else {
numCounter = 0 ;
count.text = @"0" ;
}
}
- (void)saveCount {
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"] ;
NSMutableDictionary *mDict = nil ;
if (dict == nil)
mDict = [NSMutableDictionary dictionary] ;
else
mDict = [dict mutableCopy] ;
NSString *key = [NSString stringWithFormat:@"%ld", (unsigned long)_itemIndex] ;
[mDict setObject:@(counter) forKey:key] ;
[[NSUserDefaults standardUserDefaults] setObject:mDict forKey:@"saveCount"] ;
[[NSUserDefaults standardUserDefaults] synchronize] ;
}
感謝您的幫助!
你爲什麼要註釋掉'counter = [numCounter intValue];'?我發現你將'counter'保存爲用戶默認值,但是你不用'viewDidLoad'初始化它。 –
我已經在.h文件中聲明'int counter',行'counter = [numCounter intValue]'給我一個錯誤'壞接收器類型'NSInteger'(又名「long」)',這可能會使這個問題成爲i選擇忽略計數器,不知道如何設置它沒有這個錯誤@OleksiiTaran – dicobraz