【发布时间】: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