如何使用NSMutableArray作为本地缓存

时间:2012-08-28 10:55:51

标签: objective-c ios cocoa-touch

我有一个从FlipsideViewController推送的UtilityApp和UITableViewController。我在UITableView中修改了一些单元格来嵌入UITextField和UIButton(保存按钮)。其他表格视图单元格将正常运行。单击一个普通的UITableViewCell,我将再次推送具有另一组UITableViewCells的相同UITableViewController。现在我的问题是,当我点击保存按钮时,我想在NSMUtableArray上添加一个NSObject。 当我转到下一个控制器(即,相同的UITableViewController)并单击保存按钮时,我想再次向NSMutableArray添加一个NSObject。因此,NSMutableArray对我来说就像一个本地缓存。怎么可能?基本上我想在UITableViewController之外的某个地方声明NSMutableArray并继续向它添加NSObject实例。请帮帮我。

1 个答案:

答案 0 :(得分:1)

您需要使用单例类作为缓存。例如,来自this question

@interface MySingleton : NSObject
{
}

+ (MySingleton *)sharedSingleton;
@end

@implementation MySingleton

+ (MySingleton *)sharedSingleton
{
  static MySingleton *sharedSingleton;

  @synchronized(self)
  {
    if (!sharedSingleton)
      sharedSingleton = [[MySingleton alloc] init];

    return sharedSingleton;
  }
}

@end

现在只需使用[MySingleton sharedSingleton]获取对此对象的共享引用。添加方法以向NSMutableArray添加对象。

相关问题