martti d
martti d

Reputation: 2652

How to update an object from a previous ViewController?

I have a view controller called searchUpdateController, it has a button to push a new view controller called editController. In the editController there is a button called Save and it contains the following code:



searchUpdateController = [[SearchUpdate alloc] init];
    [self.navigationController popViewControllerAnimated:YES];
    [searchUpdateController.fieldsValArray replaceObjectAtIndex:3 withObject:joinedString];
    [searchUpdateController.mainTable reloadData];
    [searchUpdateController release];

fieldsValArray is a NSMutableArray property in searchUpdateController and mainTable is the tableView in the searchUpdateController. I wanted to update the content of the fieldsValArray and reload the data of the tableView. Nothing happens when I click the Save button. Any ideas what I'm missing here?

Upvotes: 1

Views: 115

Answers (2)

Mrunal
Mrunal

Reputation: 14118

Dont do this -> searchUpdateController = [[SearchUpdate alloc] init]; Here you are creating new object, which is not referring to your older object

Before moving back to searchUpdateController do this:

  • Get second last object from self.navigationController. viewControllers, which is nothing but the searchUpdateController
  • Now you are having the same object so update the array data whichever you want
  • Now there are two possibilities to reload data:

    1. Use NSNotification and fire a notification to parent view controller where need to reload table
    2. Call a method of parent class which will reload table

Hope this is what you want.

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Here you are creating a new objet not using the old one in the code searchUpdateController = [[SearchUpdate alloc] init]; set them searchUpdateController from SearchUpdateController class. so that it point to that object that pushes the editController

Upvotes: 1

Related Questions