0

I've searched a lot and still couldn't find an answer to this...

I'm working on an iphone App (for college) in xcode 5.0 using storyboards.

I have a View Controller with a table view and everything works fine (data sources, delegate...). Items are stored in an array (call it "array1"). I have an ADD button which brings up a list of items which I want to add (if checked) to array1. I store the checked items in another array ("array2"). The thing is, when I pass array2 to the previous view controller I lose all data in array1 (it becomes nil). The code I'm using to pass data from array2 to the VC is:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"updateFavoritesSegue"]) {
    FavoritesViewController *targetVC = (FavoritesViewController*) segue.destinationViewController;
    [targetVC updateFavorites:[self newFavoritesArray]];
    }
}

The updateFavorites method is implemented as below.

-(void) updateFavorites:(NSArray *)newFavorites {
    [self.favorites addObjectsFromArray:newFavorites];
    [self.favoritesTableView reloadData];
}

Thank you very much for your help.

4
  • Is updateFavoritesSegue a push segue or an unwind segue? Commented Sep 29, 2013 at 15:29
  • 1
    You might be trying to push-segue "back" to the FavoritesViewController. That way you just create another instance of the FavoritesViewController that has nothing to do with the first one. My answer on Data-disappears-when-switching-between-view-controllers might give you an idea on what's happening. Commented Sep 29, 2013 at 15:43
  • @Jarig that's exactly what I'm doing. I'll try your solution! Thanks. Commented Sep 29, 2013 at 18:44
  • That link won't give you a solution to your problem right away. It was just supposed to clarify on the problem. If your firstVC pushes the secondVC than you will still need to let the firstVC know about the secondVC's array2. The less the secondVC knows about the firstVC the better your object-oriented design (avoiding cycles etc). Using a handler as recommended by @iiFreeman is a good way to go. Commented Sep 29, 2013 at 21:29

1 Answer 1

2

Why don't you just use some handler?

secondVC.h

@property (nonatomic, copy) void (^didFinishPickingItemsHandler)(NSArray *items);

then from the firstVC:

    - (void)showSecondScreen {
        MYSecondVC *secondVC = /* initialisation code here */
        __weak MyFirstVC *self_ = self;
        secondVC.didFinishPickingItemsHandler = ^(NSArray *items) {
           /* update you data here with your array1 and received picked items */
           [self.navigationController popViewControllerAnimated:YES];
        };
        [self.navigationController pushViewController:secondVC animated:YES];
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I'd have to do some reading on handlers, but thanks for the answer!
If you want to do some reading on that topic I can recommend A Short Practical Guide to Blocks by Apple. Blocks is a concept that's frequently used in the system framework APIs so it's worth knowing about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.