2014-12-30 19 views
0

我正在構建一個水龍頭計數的應用程序(例如:進入房間的人)。 主要目標是能夠有多個計數器(例如:主房間櫃檯,白色櫃檯櫃檯,綠色房間櫃檯等)。iOS的水龍頭計數器應用程序 - 多個計數器

該應用程序包括:

  1. 計數器的列表 - CounterListTableViewController,具有功能 在列表

  2. 創建新的項目(計數器)計數器頁 - RowCounterViewController

我困在此刻:在TableViewController中保存計數參考項目的計數器數量。

可能的解決方案:

  1. 保存所有數數到一個數組 - 我不知道如何,但(一個完整的初學者)。

  2. 按照KudoCC在下面的答案中的建議,用NSUserDefaults的索引保存所有計數數字。

謝謝!

最後編輯:

#import "RowCounterViewController.h" 

@interface RowCounterViewController() 

@end 

@implementation RowCounterViewController 

-(IBAction)plus { 
counter=counter + 1; 
count.text = [NSString stringWithFormat:@"%i",counter]; 
} 
-(IBAction)minus { 
counter=counter - 1; 
count.text = [NSString stringWithFormat:@"%i",counter]; 
} 
-(IBAction)reset { 
counter=0; 
count.text = [NSString stringWithFormat:@"%i",counter]; 
} 

- (void)viewDidLoad { 

[[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"] ; 
NSUInteger count = [dict objectForKey:@(itemIndex)] ; 

//NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
//NSString *countString = [userDefaults objectForKey:@"saveCount"]; 
//NSLog(@"Your Count: %@",count); 

//checking if data in user defaults is not empty 
//if(countString.length>0) 
//{ 
// count.text = [NSString stringWithFormat:@"%i",counter]; 
// counter = [countString intValue]; 
//} 

//else 
//{ 
// //for first time 
// counter = 0; 
// count.text = @"0"; 
//} 


//counter=0; 
//count.text = @"0"; 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
} 

-(void)saveCount { 
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"] ; 
NSMutableDictionary *mDict = [dict mutableCopy] ; 
[mDict setObject:@(count) forKey:@(itemIndex)] ; 
[[NSUserDefaults standardUserDefaults] setObject:mDict forKey:@"saveCount"] ; 
[[NSUserDefaults standardUserDefaults] synchronize] ; 
} 

//- (void)dealloc { 
// [self saveCount] ; 
//} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before 
navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
// Get the new view controller using [segue destinationViewController]. 
// Pass the selected object to the new view controller. 
} 
*/ 

@end 
+0

爲什麼要將計數保存爲字符串而不是整數?此外,如果你想多個計數器,那麼你將需要存儲一個數組,而不是一個單一的值 – Paulw11

+0

@ Paulw11多數民衆贊成那是因爲我是一個完整的初學者:)你能建議如何改變整數?我想弄清楚如何使用Ian Lan提到的方法來存儲數組 – dicobraz

+0

您可以調用'setInteger:forKey:'和'getIntgerForKey:'而不是set/get對象 – Paulw11

回答

0

您可以撥打[self saveCount]dealloc方法或viewWillDisappear:

- (void)dealloc { 
    [self saveCount] ; 
} 

或:

- (void)viewWillDisappear:(BOOL)animated { 
    [self saveCount] ; 
} 

更新:

看來你要保持計數列表中的所有項目。爲了達到這個目的,你必須用它的索引號保存NSUserDefaults中的所有計數,我建議你保存NSDictionary,它使用索引作爲它的關鍵字並且計數它的值。所以當你進入RowCounterViewController時,你應該告訴物品的索引號,這樣你可以在NSUserDefaults中得到它的計數。當您離開視圖控制器時,將其計數保存到索引。

下面的代碼用於獲取itemIndex的計數,您應該在推送或呈現之前告訴RowCounterViewController項目索引。

NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"] ; 
NSUInteger count = [[dict objectForKey:@(itemIndex)] unsignedIntegerValue] ; 

下面的代碼被用於保存計數itemIndex

-(void)saveCount { 
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCount"] ; 
    NSMutableDictionary *mDict = [dict mutableCopy] ; 
    [mDict setObject:@(count) forKey:@(itemIndex)] ; 
    [[NSUserDefaults standardUserDefaults] setObject:mDict forKey:@"saveCount"] ; 
    [[NSUserDefaults standardUserDefaults] synchronize] ; 
} 

,並在列表視圖控制器的代碼下位:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"showDetail"]) { 
     // you can fetch the selected index path in this way. 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

     RowCounterViewController *vc = (RowCounterViewController*)[segue destinationViewController]; 
     // the currentIndex is the itemIndex I mentioned above. 
     vc.currentIndex = indexPath.row; 
    } 
} 
+0

那種工作,東西是不正確的在我的代碼雖然,現在我進入櫃檯後,點擊讓我們說2次然後回到桌面,當再次去櫃檯顯示我0,但是當點擊再次它到3,所以它記住的價值,但顯示0 – dicobraz

+0

@dicobraz看來,你想保持列表中的每個項目的計數。爲了達到這個目的,你必須把所有的計數保存在'NSUserDefaults'和它的索引中,我建議你保存'NSDictionary',它使用索引作爲它的關鍵字,並將其作爲它的值。所以當你進入'RowCounterViewController'時,你應該告訴項目的索引號,所以你可以在'NSUserDefaults'中得到它的計數。當您離開視圖控制器時,將其計數保存到索引。 – KudoCC

+0

我一直在搞亂,不能得到正確的代碼,我哪裏出錯了?謝謝你的幫助!上面添加並編輯@KudoCC – dicobraz

0

,如果你有一個以上的櫃檯,你需要一個數組來存儲所有計數器,並使用UITableViewDelegate and UITableViewDatasource提出的行計數器在tableview中。

將數組保存到一個文件用這種方法

[array writeToFile:arrayFileName atomically:YES]; 

,或者你可以使用任何你能想出將其存儲在

dealloc or viewWillDisappear 

如果你希望你tableview之間進行溝通RowCounterViewController,比如RowCounterViewController將計數器編號傳回tableview並將其呈現在相關項目中,您可以使用委託或通知。

這是您將來需要學習的東西。

你可以找到一些關於它的演示。

祝你好運。

+0

多數民衆贊成什麼,我仍然需要學習,試圖找出商店陣列的方法,任何額外的幫助將不勝感激,謝謝 – dicobraz