iOS点击计数器应用程序 - 多个计数器

时间:2014-12-30 02:34:04

标签: ios uitableview nsuserdefaults

我正在构建一个用于点按计数的应用程序(例如:人们进入房间)。 主要目标是能够有多个柜台(例如:主房间柜台,白色房间柜台,绿色房间柜台等)。

该应用包含:

  1. 计数器列表 - CounterListTableViewController,带有函数 是在列表中创建新项目(计数器)

  2. 计数器页面 - RowCounterViewController

  3. 我现在处于停滞状态:参考TableViewController中的项目保存计数器的计数。

    可能的解决方案:

    1. 将所有计数数字保存到数组中 - 我还不知道(一个完整的初学者)。

    2. 使用其索引保存NSUserDefaults中的所有计数,正如KudoCC在下面的答案中所建议的那样。

    3. 谢谢!

      上次修改:

      #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
      

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

如果您有多个计数器,则需要一个数组来存储所有计数器,并使用UITableViewDelegate and UITableViewDatasource在tableview中显示行的计数器。

使用此方法将数组保存到文件

[array writeToFile:arrayFileName atomically:YES];

或者您可以使用您可以提出的任何内容将其存储在

dealloc or viewWillDisappear

如果您想在tableviewRowCounterViewController之间进行通信,请RowCounterViewController将计数器号码传回tableview并将其显示在相关项目中,您可以使用代表或通知。

这是你将来需要学习的东西。

你可以找到一些关于它的演示。

祝你好运。