1

I've tried following this post re: passing data back to a prior viewController without much luck. I've spent more time than I care to admit on this, I've read all I can, yet nothing I've found here works.

Passing Data between View Controllers

Here's what I'm trying to do:

Legend: AddPOIViewController -----------> CategoryViewController

Step 1: empty textField ---segue---> selectCategory

Step 2: setCategory on CategoryViewController

Step 3: AddPOIViewController <--delegate-- setCategory

I have an AddPOIViewController (A) and a CategoryViewController (B). I have a segue that works fine from A to B. I want to select a category in B and dump it back in A.

Here's what I've done:

AddPOIViewController.h

In my segue from AddPOIViewController to CategoryTableViewController, I set the delegate as follows:

 CategoryTableViewController *categoryTableViewController = [navigationController viewControllers][0];
 categoryTableViewController.delegate = (id<CategoryTableViewControllerDelegate>)self;


// Delegate method
 - (void)addItemViewController:(CategoryTableViewController *)controller didSelectCategory:(LocationCategory *)selectedCategory {
    NSLog(@"This was returned from CategoryTVC: %@", self.selectedCategory);
}

CategoryTableViewController.h

// This property sets CategoryTVC as the delgate of AddPOIViewController
@property (nonatomic, weak) id <CategoryTableViewControllerDelegate> delegate;

// This property is public so the AddPOIVC knows about it
@property (nonatomic, strong) LocationCategory *selectedCategory;

CategoryTableViewController.h

    - (IBAction)done:(id)sender {
        [self.delegate categoryTableViewControllerDidSave:self];    
    // This passes the value back to the AddPOIViewController
    [self.delegate addItemViewController:self didSelectCategory:self.selectedCategory];
}
Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180

1 Answers1

1

I think the problem is in the IBAction done: method in CategoryTableViewController:

- (IBAction)done:(id)sender {
    [self.delegate categoryTableViewControllerDidSave:self];    
    // This passes the value back to the AddPOIViewController
    [self.delegate addItemViewController:self didSelectCategory:self.selectedCategory];
}

The problem is that the first line of this method calls the delegate's categoryTableViewControllerDidSave: method, which dismisses the view controller:

    [self dismissViewControllerAnimated:YES completion:nil];

So the CategoryTableViewController gets dismissed before it can get to the next line, where is passes the NSManagedObject.

To fix it, just switch round the two lines in the IBAction done: method:

- (IBAction)done:(id)sender {
    // This passes the value back to the AddPOIViewController
    [self.delegate addItemViewController:self didSelectCategory:self.selectedCategory];
    // This causes this view controller to be dismissed:
    [self.delegate categoryTableViewControllerDidSave:self];    
}

You also need to save the selectedCategory that is passed in the call to the selectedCategory property of AddPOIViewController:

- (void)addItemViewController:(CategoryTableViewController *)controller didSelectCategory:(LocationCategory *)selectedCategory {
    self.selectedCategory = selectedCategory;
    NSLog(@"This was returned from CategoryTVC: %@", self.selectedCategory);
}
pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • Still `null`. I was working on it with my mentor & there was a value displayed for it when I hovered over it at the breakpoint we set, but I never looked at the log statement to see what it was printing from the `NSLog` I threw in there, though I did print it when I was hovering over it. – Adrian Apr 20 '15 at 19:46
  • 1
    Amended my answer - you need to save the value passed in the method call to the `self.selectedCategory` property. – pbasdf Apr 20 '15 at 19:51
  • Oh man! I'm going to take my beating like a man and leave this up unedited ;) Thank you for getting me squared away on this. – Adrian Apr 20 '15 at 20:23
  • 1
    No problem - we all find we can't see the wood for the trees sometimes. – pbasdf Apr 20 '15 at 20:29