【问题标题】:Can't get one a table view data from another view controller无法从另一个视图控制器获取表视图数据
【发布时间】:2014-12-21 12:15:29
【问题描述】:

我正在尝试执行一些非常简单的事情,即:

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

在主页中,我有一个 UILabel 对象,我想用表格视图控制器中的第一个单元格填充它。所以我想我需要的是在我的主页视图控制器中创建我的表视图控制器的一个实例并询问这些信息......但由于某种原因,我从名为“currentTarget”的表视图控制器获得的对象是 nil ...(事实并非如此)。

这是我的 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;
}

为什么我的标签不显示:/?

tnx 提前

【问题讨论】:

  • 你的 StackTableViewController 中的 currentTarget 是什么?
  • @property (strong, nonatomic) NSString *currentTarget;它是 .h 文件中的一个属性,抱歉
  • 和 target.body 也是一个嵌套
  • 所以我猜self.currentTarget = current.body 只调用viewDidLoad,而viewDidLoad 实际上直到视图完成加载才调用。

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


【解决方案1】:

问题在于您的 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;

【讨论】:

    【解决方案2】:

    在您的 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-11
      • 2017-03-02
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多