0

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!

Mirko Catalano
  • 3,850
  • 2
  • 26
  • 39
amitsbajaj
  • 1,304
  • 1
  • 24
  • 59

1 Answers1

1

What type do I set this to be is my first question? BOOL?

If there're only two options you may use a Boolean value. If you do that I advise to change its name to something more telling than status, for example wasBought. In my opinion it'd be even better to use enum, as in this answer.

Next, what kind of code can/should I use to get that value from the user and add it to the database?

You can do that with:

[transaction setValue:@(self.segmentedControl.selectedSegmentIndex == 0) forKey:@"wasBought"];

You might also want to read about subclassing NSManagedObject's (section "Custom Managed Object Classes").

Community
  • 1
  • 1
Arek Holko
  • 8,966
  • 4
  • 28
  • 46
  • Thank you very much Arkadiusz, I will certainly look into the Enums and subclassing (I knew I wanted to get onto that eventually - just wanted to build the basis of the app but thanks for pointing me to that documentation as well) and the wasBought is a great idea. I will jump onto this now and write in with any further issues.. thanks again! – amitsbajaj Oct 14 '13 at 16:57