我的应用程序有核心数据和两个视图CoursesTableViewController和DetailViewController。
如果用户点击Detail Disclosure按钮,则会转到DetailViewController以显示一些详细信息。
该应用程序使用didSelectRowAtIndexPath和didDeselectRowAtIndexPath进行其他操作。
CoursesTableViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setDetailItem:object];
}
}
DetailViewController.h
@property (strong, nonatomic) id detailItem;
@property (weak, nonatomic) IBOutlet UITextField *courseCodeLabel;
@property (weak, nonatomic) IBOutlet UITextField *courseNameLabel;
DetailViewController.m
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
@synthesize detailItem = _detailItem;
@synthesize courseCodeLabel = _courseCodeLabel;
@synthesize courseNameLabel = _courseNameLabel;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.courseCodeLabel.text = [[self.detailItem valueForKey:@"courseCode"] description];
self.courseNameLabel.text = [[self.detailItem valueForKey:@"courseName"] description];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.courseCodeLabel = nil;
self.courseNameLabel = nil;
}
问题是,当我点击详细信息披露时,segue工作正常,但在DetailViewController中,courseCodeLabel和courseNameLabel中没有值。
答案 0 :(得分:1)
导入&尝试转换目标视图控制器。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
DetailViewController * detailVC = (DetailViewController *)[segue destinationViewController];
[detailVC setDetailItem:object];
}
}