How to manage data between the entities with the UIPICKERVIEW?

若如初见. 提交于 2019-12-01 14:14:55

1. I would suggest you read this Official documentation on How to create, initialize and save a Managed Object.

2. Understand when you select the row in picker view then the below delegate is called, in which I can see you have used an array. This is wrong:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    categoryArray = [self.arr objectAtIndex:row];
}

From the selected row, you would get only the name of the Category because that is in the array. I guess you must be getting an error here, because you are equilating an NSString to an array. To do it correctly I would suggest you to change the previous code as well. Instead, of fetching only name property from Category entity from Core Data, you fetch the whole Category entities.

I can give you code here but that wont be any good for you. You must try first.

Fetch the whole Category Entity, use a sort desciptor for 'name'- otherwise it may create problem later and in UIPickerView- titleForRow delegate method, use dot(.) operator to display name.

3. Change your didSelectRow implementation. Donot put anything in the array. You need the selected Category to map against the newly created Product.
Create an instance variable of type Category, and using objectAtIndex method, get the corresponding Category and put it inside your instance variable.

4. In btnSave method, use the instance variable to map your Product against the selectedCategory. Please go through he link mentioned above to understand how to create, initialize and save the managed object, because I can see you are using a lot of key- value coding which is though not wrong, but is not the largely followed policy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!