Question

Sort of a simple question. I have a tableview in a navigationcontroller. When I touch a cell, it pushes a view controller with the information from the cell, so I can edit it in the new view. Now thats working correct (we can call it the informationpath: "rootviewcontroller -> pushed viewcontroller"). But when I click save in the new view, I want the edited values to travel back to the rootviewcontroller before I call popviewcontroller (informationpath: "pushed viewcontroller -> rootviewcontroller"), so the edited values can be displayed in the tableview.

Whats the correct approach to this?

EDIT:

pushViewController and popViewController is working. I only asked for the best approach to get the edited information back to the rootViewController for display in the tableview, when Save-button (popViewController) was called. I guess I'll just have to edit the pList with the new information directly from the pushed viewController. Though I would prefer sending the new information to the rootViewController and have it handle the access to the pList-file.

Was it helpful?

Solution

I have the same situation - two tableviews. The first TV displays a list of database records and when one is tapped it goes through to the second TableView which displays the details of the record. I do this by pushing the details TableViewController onto the navigation controllers stack. So far so go and quite simple.

The problem I encountered was that after updating the record in the details table view (controller), I wanted to let the list table view controller know, so that it could update the list of records.

The first thing I did was to add a property to the details table view controller so that when a row was selected on the list of records, the list controller could pass the core data managed entity to the details controller.

At the same time, I also added the list controller as a observer of core data change events like this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataSaved:) name:NSManagedObjectContextDidSaveNotification object:nil];

So if the details table view and controller update the record, the list controller has it's dataSaved: method called, passing a NSNotification object.

In the dataSaved: method I examine the object and if the core data entity being edited is in the updated list, then I set a flag to signal an update is required. However if there is a record in the inserted list, it means that a new record has been created and inserted into the database, so a flag is set to trigger a full reload of the list table view.

When the user returns to the list view controller, the viewDidAppear: method is triggered. In this method I examine the flags and either call

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

to reload the specific record if the record was updated, or tell the table view to do a full reload if there is an insert of a new record.

Finally I then remove the list controller as an observer of core data notifications because it is no longer interested.

I don't know if this is the recommended way to do this, but so far it's working for me.

OTHER TIPS

You can try to reloadData before popViewController but you should post some code.

All UIViewControllers have a navigationController method. When you push a view controller to a navigation controller, that property is set to point to the navigation controller.

So, just call [self.navigationController popViewControllerAnimated:YES]; in the view controller.

Also, don't confuse views and view controllers. When you click save in the view, you should make sure that the save button calls a method on your view controller, which in turn talks to the navigation controller.

On tapping a row You are going to new view with appropriate information.

Here you perform some operation such as editing data. So first save the changes on click on save button (By calling an IBAction) and in this IBAction method will check wheter (changes are successfully saved) then call

[self.navigationController popViewControllerAnimated:YES];

And ensure that table reload itself by updated databese (for this change array of data by calling database method in viewWillAppear method).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top