Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从数据核心ios获取数据_Ios_Database_Core Data_Fetch - Fatal编程技术网

从数据核心ios获取数据

从数据核心ios获取数据,ios,database,core-data,fetch,Ios,Database,Core Data,Fetch,我正在使用ios应用程序,当单击“添加”按钮时,它会将我带到另一个视图控制器以填写我的信息并将其保存在核心数据中。我如何获取这些数据并将其发布到Tableview中?有什么帮助吗?要保存数据(在您获取数据的视图控制器中) 在.m文件中添加 - (NSManagedObjectContext *)managedObjectContext { NSManagedObjectContext *context = nil; id delegate = [[UIApplication sh

我正在使用ios应用程序,当单击“添加”按钮时,它会将我带到另一个视图控制器以填写我的信息并将其保存在核心数据中。我如何获取这些数据并将其发布到Tableview中?有什么帮助吗?

要保存数据(在您获取数据的视图控制器中)

在.m文件中添加

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}
在保存操作中添加

- (IBAction)savePressed:(id)sender
{
    NSManagedObjectContext *context = [self managedObjectContext];

        NSManagedObject *newData = [NSEntityDescription insertNewObjectForEntityForName:@"StoredData" inManagedObjectContext:context];

        [newData setValue:textField.text forKey:@"dataattribute"];

    NSError *error = nil;

    if (![context save:&error]) {
        NSLog(@"Save Failed! %@ %@", error, [error localizedDescription]);
    }

}
在您的表视图控制器中

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}
在你看来,他确实出现了

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    NSManagedObjectContext *moc = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"entityName"];
    arrayData = [[moc  executeFetchRequest:fetchRequest error:nil] mutableCopy];


    [self.tableView reloadData];
}
在你的牢房里

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    NSManagedObject *data = [arrayData objectAtIndex:indexPath.row];

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [data valueForKey:@"dataattribute"]]];

    return cell;
}

你读过这方面的教程吗?^^了解
NSFetchRequest
NSFetchedResultsController
以便于处理;)好的,谢谢。我的问题是它们是两个不同的视图控制器,我不知道如何使用另一个视图控制器中的对象。这是基础知识。^^使用谷歌或阅读苹果API;)有很多免费代码示例可以帮助您。只需使用internet上现有的源:)对于
NSManagedObjectContext
,您将需要一个单例,或者可以直接编写一个ModelHandler类(作为单例),用于处理所有核心数据操作。有很多方法可以做,只要向谷歌索要核心数据的示例项目;)谢谢,我的问题是它们是两个不同的视图控制器,我不知道如何从另一个视图使用对象controller@mowi请查看我的编辑,我添加了如何从一个视图控制器保存数据,然后将其加载到表视图对不起,我的问题可能很愚蠢,但我可以知道“arraydata”吗如何使用?@mowi arrayData是NSMutableArray,您应该在.h文件中声明它。无法将标识符为cell的单元格出列-必须为标识符注册nib或类,或者在情节提要中连接原型单元格