Question

I have 2 ViewControllers

ViewControllerWithCollectionView (FIRST) and ModalViewControllerToEditCellContent (SECOND)

I segue from FIRST to SECOND modally. Edit cell. Return.

After dismissing SECOND controller, edited cell doesn't get updated until i call [collection reloadData]; somewhere manually.

Tried to put it in viewWillAppear:animated:, when i check log, it's not called (after dismissing SECOND)

I've tried various solutions, but i can't brake thru (maybe I'm just too exhausted). I sense that I'm missing something basic.

EDIT dismiss button

- (IBAction)modalViewControllerDismiss  
    {
        self.sticker.text = self.text.text; //using textFields text
        self.sticker.title = self.titleText.text;// title

        //tried this also
        CBSStickerViewController *pvc = (CBSStickerViewController *)self.stickerViewController; 
        //tried passing reference of **FIRST** controller
        [pvc.cv reloadData];//called reloadData
        //nothing

        [self dismissViewControllerAnimated:YES completion:^{}];
    }
Was it helpful?

Solution

It's tough to tell from the posted code what's wrong with the pointer to the first view controller that you passed to the second. You should also be able to refer in the second view controller to self.presentingViewController. Either way, the prettier design is to find a way for the first view controller to learn that a change has been made and update it's own views.

There are a couple approaches, but I'll suggest the delegate pattern here. The second view controller can be setup to have the first view controller do work for it, namely reload a table view. Here's how it looks in almost-code:

// SecondVc.h

@protocol SecondVcDelegate;

@interface SecondVC : UIViewController

@property(weak, nonatomic) id<SecondVcDelegate>delegate;  // this will be an instance of the first vc

// other properties

@end

@protocol SecondVcDelegate <NSObject>

- (void)secondVcDidChangeTheSticker:(SecondVc *)vc;

@end

Now the second vc uses this to ask the first vc to do work for it, but the second vc remains pretty dumb about the details of the first vc's implementation. We don't refer to the first vc's UITableView here, or any of it's views, and we don't tell any tables to reload.

// SecondVc.m

- (IBAction)modalViewControllerDismiss {

    self.sticker.text = self.text.text; //using textFields text
    self.sticker.title = self.titleText.text;// title

    [self.delegate secondVcDidChangeTheSticker:self];
    [self dismissViewControllerAnimated:YES completion:^{}];
}

All that must be done now is for the first vc to do what it must to be a delegate:

// FirstVc.h

#import "SecondVc.h"

@interface FirstVc :UIViewController <SecondVcDelegate>  // declare itself a delegate

// etc.

// FirstVc.m

// wherever you decide to present the second vc
- (void)presentSecondVc {

    SecondVc *secondVc = // however you do this now, maybe get it from storyboard?
    vc.delegate = self;  // that's the back pointer you were trying to achieve

    [self presentViewController:secondVc animated:YES completion:nil];
}

Finally, the punch line. Implement the delegate method. Here you do the work that second vc wants by reloading the table view

- (void) secondVcDidChangeTheSticker:(SecondVc *)vc {

    [self.tableView reloadData];  // i think you might call this "cv", which isn't a terrific name if it's a table view
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top