0

I have tableViewController class and want to pass a property to another view controller.

I declared my property,

@property (strong, nonatomic) NSIndexPath *selection;

In protocol tableView:didSelectRowAtIndexPath:, I want to set my selection value into indexPath selected.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // change your selection property
    [self setSelection:indexPath];
}

I want to pass that selection into destination view controller by prepareForSegue:sender:

if ([segue.identifier isEqualToString:@"updateItem"]) {
        UINavigationController *navController = (UINavigationController *)[segue destinationViewController];
        UpdateViewController *viewController = (UpdateViewController *)[navController topViewController];

        [viewController setManagedObjectContext:self.managedObjectContext];

        NSManagedObject *record = [self.fetchResultsController objectAtIndexPath:self.selection];
        if (self.selection) {
            [viewController setRecord:record];
        }

        // reset selection
        [self setSelection:nil];
    }

My problem is, I NSlog-ing that property on destination view controller and for the first time it showed as null. Secondly, it showed as it set for the first, and so on and so on. It showed what I set before.

So did I miss the concept of setting a property before it goes into another view controller destination? Please let me know this. Any help would be appreciated. Thank you so much.

Sonic Master
  • 1,238
  • 2
  • 22
  • 36
  • where did you allocated 'selection' – Rohit Pradhan Feb 11 '16 at 12:48
  • I haven't allocate 'selection'. Just declare it as property in my private interface, and set it on my didSelectAtIndex protocol. – Sonic Master Feb 11 '16 at 12:50
  • @RohitKP should i alloc init 'selection'? – Sonic Master Feb 11 '16 at 12:58
  • Could you show logging code? – Maxim Sysenko Feb 11 '16 at 12:59
  • In viewDidLoad's destination controller > NSLog(@"Content: %@", [self.record valueForKey:@"name"]); @MaximSysenko – Sonic Master Feb 11 '16 at 13:00
  • At first, need to add additional NSLog before `if (self.selection) {`. By the way, why not to check for `recording` object in if statement? – Maxim Sysenko Feb 11 '16 at 13:05
  • NSLog before check 'self.selection' give me null for first try. I think my prepareForSegue runs before my didSelectRowAtIndexPath that set my 'selection'. @Maxim Sysenko – Sonic Master Feb 11 '16 at 14:19
  • You also could check it by setting breakpoints in prepareForSegue and didSelect methods. How you trigger your segue? @SonicMaster – Maxim Sysenko Feb 11 '16 at 21:38
  • Ah, I checked it by using breakkpoint and my `prepareForSegue` run before my `didSelect`. I try to `performSegueWithIdentifier` on my `didSelectRowAtIndexPath` too, but it still the same. My `prepareForSegue` run before my `didSelect`, @MaximSysenko – Sonic Master Feb 12 '16 at 01:34
  • Hi, @MaximSysenko! I think I messed up with segue, I should use either `performSegueWithIdentifier` or `didSelectRowAtIndexPath`. I decide to use`performSegueWithIdentifier`, so I set `selection` there by `[[self tableView] indexPathForSelectedRow]`. Thank you so much, Maxim. Now it is solved. – Sonic Master Feb 12 '16 at 02:20
  • Instead of editing your question, feel free to _answer_ your own question! That is perfectly legal on Stack Overflow. You can even accept your own answer in 2 days, to show everyone that the problem is solved. – matt Feb 12 '16 at 02:29

1 Answers1

0

I finally found the answer. I messed up with how to trigger segue. prepareForSegue runs first before didSelectRowAtIndexPath.

Refer to this we should use either of prepareForSegue or didSelectRowAtIndexPath. So I decide to set my selection in prepareForSegue by get its indexPathForSelectedRow.

[self setSelection:[[self tableView] indexPathForSelectedRow]
Community
  • 1
  • 1
Sonic Master
  • 1,238
  • 2
  • 22
  • 36