I have a UITableViewController class and I want to save its information using NSUserDefaults. My table is created through an array called "tasks" which are objects from an NSObject class "New Task". How and where do I use NSUserDefaults? I know that I have to add my array as a NSUserDefaults object, but how do I go about retrieving it? Any help would be appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoneCellIdentifier = @"DoneTaskCell";
static NSString *NotDoneCellIdentifier = @"NotDoneTaskCell";
NewTask* currentTask = [self.tasks objectAtIndex:indexPath.row];
NSString *cellIdentifer = currentTask.done ? DoneCellIdentifier : NotDoneCellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer forIndexPath:indexPath];
if(cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
}
cell.textLabel.text = currentTask.name;
return cell;
}
This is my viewdidload method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tasks = [[NSMutableArray alloc]init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.tasks forKey:@"TasksArray"];
}