如何在标签中显示核心数据

时间:2014-10-11 05:41:46

标签: ios core-data

我从核心数据中获取了结果,但我不知道如何在视图中显示获取的数据。大多数查询都说明如何在表格视图中显示,但我需要在视图中的标签中显示。

- (IBAction)btnSubmit:(id)sender
{

     AppDelegate *appDelegate =
    [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context =
    [appDelegate managedObjectContext];

    NSEntityDescription *entityDesc =
    [NSEntityDescription entityForName:@"School"
                inManagedObjectContext:context];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    NSPredicate *pred =
    [NSPredicate predicateWithFormat:@"(mailId = %@)",
     _txtMailId.text];
    [request setPredicate:pred];
   // NSManagedObject *matches = nil;

    NSError *error;
    NSArray *objects = [context executeFetchRequest:request
                                              error:&error];

    if ([objects count] == 0)
    {
        _lblInvalidPswrd.alpha = 1.0;
        _lblPassword.alpha = 0.0;
        _lblPswrdIndication.alpha = 0.0;

    }
    else
    {
        School *info = _fetchedResultsController;
        _lblPassword.text = info.passWord;
        _lblPassword.alpha = 1.0;
        _lblPswrdIndication.alpha = 1.0;
        _lblInvalidPswrd.alpha = 0.0;
       // matches = objects[0];

    }
}

- (NSFetchedResultsController *)fetchedResultsController
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = appDelegate.managedObjectContext;
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"School" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"passWord" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    [fetchRequest setFetchBatchSize:20];
    SignUpViewController *sign = [[SignUpViewController alloc]init];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(passWord = %@)",sign.txtPswrd.text];
    NSFetchedResultsController *theFetchedResultsController =[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil
                                                                                                            cacheName:nil]; // better to not use cache
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

1 个答案:

答案 0 :(得分:0)

objects获得executeFetchRequest数组后,您就拥有了所需的数据 - 您不需要FetchedResultsController。如果您感兴趣的是第一个结果,您可以这样做 -

...

NSArray *objects = [context executeFetchRequest:request
                                              error:&error];

    if ([objects count] == 0)
    {
        _lblInvalidPswrd.hidden = NO;
        _lblPassword.hidden = YES;
        _lblPswrdIndication.hidden = YES;

    }
    else
    {
        School *info = objects[0];
        _lblPassword.text = info.passWord;
        _lblPassword.hidden = NO;
        _lblPswrdIndication.hidden = NO;
        _lblInvalidPswrd.hidden = YES;

    }

   ...

我假设School是您的NSManagedObject子类