0
我有一個核心數據文件與 類別實體,它有2個屬性 - ID和名稱。核心數據簡單獲取和更新
有人可以提供一種方法來從這兩個屬性獲取值,並更新它們。我試過了,失敗了。
這裏是我當前的代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addCategory:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
}
-(IBAction)addCategory:(id)sender
{
UIAlertView *addCategoryAlertView = [[UIAlertView alloc] initWithTitle:@"Add Category" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
addCategoryAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
self.textField = [addCategoryAlertView textFieldAtIndex:0];
self.textField.placeholder = @"Enter category name";
[addCategoryAlertView show];
}
,並在UIAlertView中的委託方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
我一定值添加到這些類別。
下面是我已經做到了現在:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == kAddCategoryButtonIndex) {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSError *error;
//NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Categories" inManagedObjectContext:context];
//[fetchRequest setEntity:entity];
[entity setValue:self.textField.text forKey:@"name"];
NSLog(@"%@",[entity valueForKey:@"name"]);
[context save:&error];
}
}
而且我認爲它的工作原理,但我不能讓這些2個屬性的值。
如果有人能幫助我,那會很棒。謝謝。