无法从另一个视图控制器获取一个表视图数据

时间:2014-12-21 12:15:29

标签: ios objective-c iphone core-data nsfetchedresultscontroller

我试图执行一些非常简单的事情:

我有一个主页视图控制器和一个支持删除单元格的表视图控制器(单独的控制器)。

在主页中,我有一个UILabel对象,我想用表视图控制器中的第一个单元格填充它。所以我想我需要的是在我的主页视图控制器中创建一个我的表视图控制器的实例并询问这些信息......但是由于某种原因,我从表视图控制器获取的对象称为&#34 ; currentTarget当前"没有......(而且它没有)。

这是我的HomeViewController :(只是相关的行)

#import "StackTableViewController.h"

@interface HomeViewController ()

@property (strong, nonatomic) IBOutlet UILabel *homeLabel;

@end

@implementation HomeViewController

- (id)init {
    self = [super initWithNibName:@"HomeViewController" bundle:nil];
    if (self) {
        // Do something
    }
    return self;
}

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationController setNavigationBarHidden:YES];
    self.homeLabel.font = [UIFont fontWithName:@"Candara-Bold" size:40];

    StackTableViewController *svc = [[StackTableViewController alloc] init];
    self.homeLabel.text = svc.currentTarget;
}

这是我的StackTableViewController.m :(只是相关的行)

#import "StackTableViewController.h"
#import "Target.h"
#import "StackTableViewCell.h"
#import "HomeViewController.h"
#import "CoreDataStack.h"

@interface StackTableViewController () <NSFetchedResultsControllerDelegate>

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultController;

@end

@implementation StackTableViewController

- (id)init {

    self = [super initWithNibName:@"StackTableViewController" bundle:nil];
    if (self) {
        // Do something

    }
    return self;
}


- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    [self.fetchedResultController performFetch:nil];

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];

    Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];

    self.currentTarget = current.body;

}

并且还在准备segue方法中添加了此代码,因此currentTarget将从segue返回更新:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    [self.fetchedResultController performFetch:nil];

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];

    Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];

    self.currentTarget = current.body;
}

为什么我的标签不会出现:/?

提前

2 个答案:

答案 0 :(得分:2)

问题是你的init方法没有初始化获取的结果控制器或currentTarget属性。它们只是初始化当您呈现该表视图控制器并运行viewDidLoad方法时。如果将这些行移动(或复制)到init方法,它应该可以工作:

[self.fetchedResultController performFetch:nil];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
self.currentTarget = current.body;

答案 1 :(得分:1)

在StackTableViewController.m中:

- (id)init {
    self = [super initWithNibName:@"HomeViewController" bundle:nil];
    if (self) {
      [self.fetchedResultController performFetch:nil];
      NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
      Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
      self.currentTarget = current.body;    }
    return self;
}