0

I am new to programming in Xcode and I am having a problem in my app (a ToDo list). Everything works just fine but when the app is quit (Not minimised) the main view controller doesn't save what is on it (A problem if you have a ToDo list). Now I want to know what code I would have to implement in order to preserve the state of a view controller upon exit and where? (The app delegate or in my main view controller window)

.m file:

@implementation RHTaskListViewController

@synthesize tasks = _tasks;

-(id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self)
    {
        //custom
    }
    return self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tasks = [[NSMutableArray alloc] init];
}




- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

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

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

//Segue from Add task to task list
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddTaskSegue"])
    {

        UINavigationController *navCon = segue.destinationViewController;

        RHAddTaskViewController *addTaskViewController = [navCon.viewControllers objectAtIndex:0];

        addTaskViewController.taskListViewController = self;
    }


    else if ([segue.identifier isEqualToString:@"EditDoneTaskSegue"] || [segue.identifier isEqualToString:@"EditNotDoneTaskSegue"])
    {
        RHEditTaskViewController *editTaskViewController = segue.destinationViewController;
        editTaskViewController.task = [self.tasks objectAtIndex:self.tableView.indexPathForSelectedRow.row];
    }
}
//Segue from Add task to task list


//Move Items
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath         toIndexPath:(NSIndexPath *)toIndexPath
{
    RHTask *movedTask = [self.tasks objectAtIndex:fromIndexPath.row];
    [self.tasks removeObjectAtIndex:fromIndexPath.row];
    [self.tasks insertObject:movedTask atIndex:toIndexPath.row];
}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//Move Items


//Delete Items
-(void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.tasks removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]     withRowAnimation:UITableViewRowAnimationFade];
    }

    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {

    }
}
//Delete Items


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}    

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tasks.count;
}



-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NotDoneCellIdentifier = @"NotDoneTaskCell";
static NSString *DoneCellIdentifier = @"DoneTaskCell";

RHTask *currentTask = [self.tasks objectAtIndex:indexPath.row];

NSString *cellIdentifier = currentTask.done ? DoneCellIdentifier : NotDoneCellIdentifier;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
//Saving the tasks

//[[NSUserDefaults standardUserDefaults] setString:saveTask forKey:@"taskSaved"];


//Saving the tasks

cell.textLabel.text = currentTask.name;


return cell;
}



#pragma mark - IBActions

-(void)editButtonPressed:(id)sender
{
    self.editing = !self.editing;
}

@end

.h file:

#import <UIKit/UIKit.h>

@interface RHTaskListViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *tasks;

-(IBAction)editButtonPressed:(id)sender;

@end

2 Answers2

2

U can use NSUserDefaults to achieve your goal.

Take this tutorial link

Main code:

For saving:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"key1" forKey:@"todo1"];
[defaults synchronize]; // do NOT forget this line!

For retrieving:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *todo1 = [defaults objectForKey:@"key1"];

Just keep in mind , u can save NSArray, NSDictionary, NSString, NSNumber and all object that conforms to NSCoping protocol.

gran33
  • 12,421
  • 9
  • 48
  • 76
  • I added your code and it gave me an error on: NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; The error message is: Redefinition of 'defaults' – user3886584 Jul 29 '14 at 10:58
  • That is exactly what it said... Redefinition of 'defaults' – user3886584 Jul 29 '14 at 11:43
  • It came up because defaults was defined twice. But then where do I put those lines of code you gave me? In the main viewcontroller.m that I am trying to save the state of? And where in the .m file? – user3886584 Jul 29 '14 at 16:03
  • U can put this code lines where ever u want to write to the todo list and where ever u want to retrieve the todo list. – gran33 Jul 30 '14 at 06:43
1

As gran33 already noted you can use NSUserDefaults to store your app data.

If your todo list more complex than just a list of tasks (i.e. each task has more properties like time,description,location etc...) I recommend using core data to store your data. Take a look here for a CoreData tutorial.

giorashc
  • 13,691
  • 3
  • 35
  • 71