0

ビュー コントローラー間でデータを渡す際に問題が発生しています。

3 つのビュー コントローラーはすべてテーブル ビューです。

 WorkoutTypeVC
 WorkoutSetVC
 WorkoutExerciseVC

私には3つのエンティティがあり、

WorkoutType
    workouts(->WorkoutSet) One to Many
WorkoutSet
    exercises(->WorkoutExercise) One to Many
    workoutType(->WorkoutType) Inverse
WorkoutExercise
    workoutSet(->WorkoutSet) Inverse

3 つすべてのビュー コントローラーを切り替えることができます。WorkoutTypeVC が読み込まれ、すべてのエントリが正しく表示されます。選択すると、WorkoutSetVC が読み込まれ、WorkoutTypeVC からの選択に対応する正しいエントリが表示されます。

しかし、WorkoutSetVC からエントリを選択すると、WorkoutExerciseVC は読み込まれますが空で、選択のタイトルも読み込まれません。

WorkoutTypeVC と WorkoutSetVC から切り替えるときに使用したのと同じコードを使用しました。

以下は、WorkoutType.m ファイルでビューを切り替えるためのコードです。

-(void)fetchWorkoutTypes
{
    NSFetchRequest *fetchRequest =
    [NSFetchRequest fetchRequestWithEntityName:@"WorkoutType"];
    NSString *cacheName = [@"WorkoutType" stringByAppendingString:@"Cache"];
    NSSortDescriptor *sortDescriptor =
    [NSSortDescriptor sortDescriptorWithKey:@"workoutType" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    self.fetchedResultsController = [[NSFetchedResultsController alloc]
                                 initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext
                                 sectionNameKeyPath:nil cacheName:cacheName];
    NSError *error;
    if (![self.fetchedResultsController performFetch:&error])
    {
        NSLog(@"Fetch failed: %@", error);
    }
}

- (void)viewDidAppear:(BOOL)animated{

    [self fetchWorkoutTypes];
    [self.tableView reloadData];
}
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return self.fetchedResultsController.fetchedObjects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                  reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController
                                        objectAtIndexPath:indexPath];
    cell.textLabel.text = workoutType.workoutType;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", workoutType.workouts.count];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    WorkoutSetViewController *detailViewController = [[WorkoutSetViewController alloc] initWithWorkoutType:workoutType];

    [self.navigationController pushViewController:detailViewController animated:YES];

}

以下はWorkoutSetVC.mのコードです

-(void)fetchWorkoutSets
{
    NSFetchRequest *fetchRequest =
    [NSFetchRequest fetchRequestWithEntityName:@"WorkoutSet"];
    NSString *cacheName = [@"WorkoutSet" stringByAppendingString:@"Cache"];
    NSSortDescriptor *sortDescriptor =
[NSSortDescriptor sortDescriptorWithKey:@"workoutName" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    self.fetchedResultsController = [[NSFetchedResultsController alloc]
                                 initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext
                                 sectionNameKeyPath:nil cacheName:cacheName];
    NSError *error;
    if (![self.fetchedResultsController performFetch:&error])
    {
        NSLog(@"Fetch failed: %@", error);
    }
}
- (id)initWithWorkoutType:(WorkoutType *)workoutType
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self)
    {
        self.workoutType = workoutType;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = self.workoutType.workoutType;

    [self fetchWorkoutSets];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.workoutType.workouts.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                  reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    WorkoutSet *workoutSet = [self.workoutType.workouts.allObjects objectAtIndex:indexPath.row];
    cell.textLabel.text = workoutSet.workoutName;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", workoutSet.exercises.count];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorkoutSet *workoutSet = (WorkoutSet *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    WorkoutExerciseTableViewController *detailViewController = [[WorkoutExerciseTableViewController alloc] initWithWorkoutSet:workoutSet];

    [self.navigationController pushViewController:detailViewController animated:YES];
}

以下は WorkoutExercise.m のコードです。

- (id)initWithWorkoutSet:(WorkoutSet *)workoutSet
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self)
    {
        self.workoutSet = workoutSet;
    }
    return self;
}

-(void)fetchWorkoutExercises
{
    NSFetchRequest *fetchRequest =
   [NSFetchRequest fetchRequestWithEntityName:@"WorkoutExercise"];
   NSString *cacheName = [@"WorkoutExercise" stringByAppendingString:@"Cache"];
    NSSortDescriptor *sortDescriptor =
    [NSSortDescriptor sortDescriptorWithKey:@"exerciseName" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    self.fetchedResultsController = [[NSFetchedResultsController alloc]
                                 initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext
                                 sectionNameKeyPath:nil cacheName:cacheName];
    NSError *error;
    if (![self.fetchedResultsController performFetch:&error])
    {
        NSLog(@"Fetch failed: %@", error);
    } 
 }

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = self.workoutSet.workoutName;
    [self fetchWorkoutExercises];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                  reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    WorkoutExercise *exercise = [self.workoutSet.exercises.allObjects objectAtIndex:indexPath.row];

    cell.textLabel.text = exercise.exerciseName;

    return cell;
}

3 番目のビュー コントローラーがすべてのエントリを一覧表示するために何をする必要があるかわかりません。ViewDidLoad メソッドでコーディングされている 3 番目のビュー コントローラーのタイトルも読み込まれません。

ありがとうございました

4

1 に答える 1