0
votes

In my app, I am listing core data entries in a tableview. I want to allow the user to edit records using a detail view presented modally as a form view. I am observing peculiar behavior when editing records.

The flow:

  1. User loads tableview with records. -working
  2. User selects a record for editing. -working
  3. User edits record in a view controller presented as a modal form view. -working
  4. User saves edits and dismisses form view. -working
  5. Tableview shows correct changes for the previously edited record. -working
  6. User repeats steps 2 - 4 selecting a different record to edit. -working
  7. Tableview shows correct data for all records. -Not Working.

At step 7, the tableview reverts the display of the first edited record to its original state. Subsequent record edits result in all previous edits reverting to their original state. If the tableview is dismissed and reloaded, the records are correct, showing all the edits.

I have used [tableview reload] in the tableview's ViewWillAppear method, but it does not seem to be fired when the modal form view controller is dismissed.

In my tableviewcontroller code:

-(void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
}

Searching around, I have not found a solution and am hoping someone can point me in the right direction.

Thanks!

2
You should have [super viewWillAppear:animated] in your implementation. I don't know if its absence would cause viewWillAppear not to be called. It certainly should be when you dismiss the modal.rdelmar
Thanks. I assume you would put this in the modal's dismiss method... It does not seem to work. Might need to setup a delegate method for this i guess... The modal is called using a segue and the tableview is not currently the delegate for the modal.BBruce
No, it goes in viewWillAppear. You should always call super in those methods (viewDidLoad, viewDidAppear, etc.).rdelmar
So, like this: -(void)viewWillAppear:(BOOL)animated { [super ViewWillAppear:animated]; [self.tableView reloadData]; }BBruce
Yes, that's correct (except for the capital "V" in view).rdelmar

2 Answers

4
votes

When you presenting a modal view, main view controller's view never disappears. So, after you dismiss your modal view, viewWillAppear() won't be called.

You could try to implement a custom delegate function in your modal view, and set it in the main view controller, when your data is updated, fire the delegate function to reload your tableView, which is at your main viewController.

Understand what is delegate function in iOS, and

how to create a delegate function is like this:

  1. In your ModalView.h:

    // define the protocol for the delegate
    @protocol ModelViewDelegate <NSObject> 
      - (void) didUpdateData;
    @end
    
    @interface ModalView: ViewController {
         //create a delegate instance
         id delegate;
    }
    
    // define delegate instance
    @property (nonatomic, assign) id <ModelViewDelegate> delegate;
    
  2. In your modalView.m:

    @synthesize delegate;
    

    and then inside your function, put the delegate function at place where you need to fire, for example:

    - (void) updateDataIntoDatabase{
           ....
           //Update work done.
    
           [self.delegate didUpdateData];
           //dismiss your modalView;
    }
    

So, in your MainViewController.h,

#import ModalView.h

and

@interface ModalView: ViewController <ModelViewDelegate > {
      ...
}

Inside your MainViewController.m, you will get a warning saying that you need to implement the delegate function which you already declared. So, set the delegate function, and do the things that you like to do:

- (void) didUpdateData{
    [self.tableView reloadData];
}

DON'T forget to set your modelView delegate to self after you instantiate the modalView instance. If not your delegate function won't be fired.

modalView.delegate = self;
0
votes

Use completion block when you trigger the Modal viewcontroller:

-(void)editModal:(id)sender
{
    KDSecondViewController *secondVC = [[KDSecondViewController alloc] init];
    [self presentViewController:secondVC animated:YES completion:^{
        //-- reload your table view when user dismiss the modal view
        [self.tableView reloadData];
    }];
}