Hi all,
In my most recent app project, I decided to go along and learn to use Core Data for storage. There is no reason I could not do it with plain old sqlite, I just wanted to try out the Core Data framework.
I have, in my view controller, an UISegmentedControl with only two possible choices (labeled "Yes" and "No"). There is a save button connected in IB to the following method:
If you will notice the else clause, I set the variable x to the value of the selected index of the UISegmentedControl. There are no error messages, but the value is always saved as 1 (I checked the .sqlite file used as the persistent store), even though x =0 (I checked by placing a breakpoint there) when the other segment is selected. Could someone help me decipher this behavior?
-- edit ---
Actually, when the else clause executes, no changes are saved at all, not even the other controls, nor the desc field. It works fine when self.action==@"new".
In my most recent app project, I decided to go along and learn to use Core Data for storage. There is no reason I could not do it with plain old sqlite, I just wanted to try out the Core Data framework.
I have, in my view controller, an UISegmentedControl with only two possible choices (labeled "Yes" and "No"). There is a save button connected in IB to the following method:
Code:
-(void)create_task
{
[txtDescription resignFirstResponder];
CoreDataLayer *dl = self.dataLayer; //this is a wrapper class where my data context is stored
NSManagedObjectContext* context = dl.context;
NSEntityDescription* tareaDescription =[dl descriptionForEntityName:@"Task"];
if (self.action ==@"new")
{
NSManagedObject* newTask = [[NSManagedObject alloc] initWithEntity:tareaDescription insertIntoManagedObjectContext:context];
NSNumber *comp = [NSNumber numberWithInt:segCompleted.selectedSegmentIndex];
[newTask setValue:txtDescription.text forKey:@"desc"];
[newTask setValue:[dateAssignedControl date] forKey:@"date_assigned"];
[newTask setValue:[dateCompControl date] forKey:@"date_completed"];
[newTask setValue: comp forKey:@"completed"];
[self.dataLayer.context insertObject:newTask];
}
else
{
[self.taskToEdit setValue:txtDescription.text forKey:@"desc"];
[self.taskToEdit setValue:[dateAssignedControl date] forKey:@"date_assigned"];
[self.taskToEdit setValue:[dateCompControl date] forKey:@"date_completed"];
NSNumber * comp =[NSNumber numberWithInt:segCompleted.selectedSegmentIndex]; //this is the segmented control
int x = [comp intValue];
self.taskToEdit.completed = comp;
}
NSError *error;
[dl.context save:&error];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Task has been saved." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[self.navigationController popToRootViewControllerAnimated:YES];
}
If you will notice the else clause, I set the variable x to the value of the selected index of the UISegmentedControl. There are no error messages, but the value is always saved as 1 (I checked the .sqlite file used as the persistent store), even though x =0 (I checked by placing a breakpoint there) when the other segment is selected. Could someone help me decipher this behavior?
-- edit ---
Actually, when the else clause executes, no changes are saved at all, not even the other controls, nor the desc field. It works fine when self.action==@"new".
Last edited: