0

I am trying to use the value from a UIPicker in a core-data app.

The picker is filled with items from entity B (categories).

I want to have the record in entity A use the selected item from entity B.

I can load the picker, and by logging, I can see the picker values, e.g.:

cat is Insurance,

cat is Travel.

cat is a relationship to the main entity (expense), I don't know how to link the one to the other.

I've been trying some of these:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    //NSString *cat = [expense valueForKey:@"category"];
    //NSLog(@"cat is %@", cat);

    NSString *cat2 = ((SPCategory *)[allCategories objectAtIndex:row]).name;
    NSLog(@"cat2 is %@", cat2); << this works I can see the cat name.

    self.expense.category.name = ((SPCategory *)[allCategories objectAtIndex:row]).name;
    NSLog(@"category is %@", self.expense.category.name );

    //categoryTextField.rightTextField.text = [self.expense valueForKey:@"category"];

}

Thanks for any help..

ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • Wouldn't you just set the category to the selected one, not the name? `self.expense.category = ((SPCategory *)[allCategories objectAtIndex:row])` – jlujan Dec 31 '12 at 21:13
  • Thanks for answering. Could you please be specific (code snippet?). I've burned out my brain. – ICL1901 Dec 31 '12 at 21:14
  • I didnt see your snippet. Thanks!!. But now, I get this exception: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SPCategory isEqualToString:]: unrecognized selector sent to instance 0x1cda54f0'. Maybe this is something different though. So if you put you comment as an answer, I'll accept it.. If you think it's the same issue, then I'd be most grateful for any help.. – ICL1901 Dec 31 '12 at 21:21
  • I doubt the exception is caused by the code you have posted. You can get more insight using an exception breakpoint, see http://stackoverflow.com/a/384795/555425 – jlujan Dec 31 '12 at 21:30

1 Answers1

1

Try self.expense.category = ((SPCategory *)[allCategories objectAtIndex:row]).

jlujan
  • 1,188
  • 7
  • 10
  • Thank you. that solved the problem getting the category to appear and be selected as a picker item. But when I go back to the RootVC, I get an exception trying to populate the cell text field. Any idea, or should I ask a new question? Currently, I have cell.detailTextLabel.text = [exp valueForKey:@"category"]; – ICL1901 Dec 31 '12 at 21:32
  • 1
    My wild guess would be `cell.detailTextLabel.text = ((SPCategory*)[exp valueForKey:@"category"]]).name`? Not sure what exp is though. – jlujan Dec 31 '12 at 21:56
  • yea, I had things working but changed category to be a relation to an expense rather than an attribute. It seems that I have some code to fix.. Oh well, it's not like it's new years eve or anything.. oh wait.. – ICL1901 Dec 31 '12 at 22:17