私のアプリにはコアデータと2つのビューCoursesTableViewControllerとDetailViewControllerがあります。
ユーザーがDetailDisclosureボタンをタップすると、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;
}
問題は、Detail Disclosureをタップすると、セグエは正常に機能しますが、DetailViewControllerではcourseCodeLabelとcourseNameLabelに値がありません。