I am progressing nicely within my basic first application; I am fairly new to App development but have been working on it for quite some time. I have tried to find an answer to this question that just didn't make sense to me.
I have a single UITableViewController
which has a plus button that points to a ModalViewController
; this asks the user to fill in 3 fields with text and select "Bought or Sold" which I've set up as a UISegmentedController
in the UI with the first button saying "Bought" and the second saying "Sold".
My class for adding the values of the three text fields contains the following code and when I click Save, it returns to the UITableView
and displays the typed content.
I would like to set the cell of be one color for Bought and one color for Sold. I can do that easily enough with the cellForRow, etc. The problem is the data model and the code which I need help with.
I have a data model with the following Entities:
- Transaction, Person, Occasion and Amount
Transaction has a relationship to Person, Occasion and Amount. It also has a "status" attribute which is the result of the UISegmentedControl
.
What type do I set this to be is my first question? BOOL?
Next, what kind of code can/should I use to get that value from the user and add it to the database?
This is the current save method for the textFields:
- (IBAction)save:(id)sender
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *transaction = [NSEntityDescription insertNewObjectForEntityForName:@"Transaction" inManagedObjectContext:context];
NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
NSManagedObject *occasionEvent = [NSEntityDescription insertNewObjectForEntityForName:@"Occasion" inManagedObjectContext:context];
NSManagedObject *amountType = [NSEntityDescription insertNewObjectForEntityForName:@"Gift" inManagedObjectContext:context];
[person setValue:self.nameTextField.text forKey:@"name"];
[occasionEvent setValue:self.occasionTextField.text forKey:@"title"];
[amountType setValue:self.amountTextField.text forKey:@"amount"];
[transaction setValue:person forKey:@"whoBy"];
[transaction setValue:occasionEvent forKey:@"occasion"];
[transaction setValue:amountType forKey:@"gifting"];
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Can't save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Any help would be massively appreciated!