"Appearing and disappearing views"


In this section, when we override viewWillDisappear:, we are setting DetailViewController’s ivar to correspond to the text fields. How does that information get transferred back over to ItemsViewController so it can update itself?

The line [[self tableView] reloadData] reloads the table, but how does ItemsViewController’s ivar get updated?


you can read it in the book. When a UIViewController is about to be popped out of the stack it receives a message: viewWillDisappear:

You can implement this method to set the properties of the “item” instance.

Remember that the object “item” is an instance of the BNRItem class, so it has properties that you can set up with the values of the text fields.

in fact, you do that when you put this on the code

-(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear]; item.itemName = nameField.text; item.serialNumber = serialNumber.text; item.valueInDollars = valueField.text.intValue; }

once you do that and the user press the “back” button, the values of the “item” object will be updated and the controller whose view is in the top of the stack may use it to update it fields.


I get that it’s updated in -viewWillDisappear, but to me, that seems like it only updates the instance of BNRItem to which DetailViewController points. How does that affect ItemsViewController’s knowledge?

EDIT: Is it because the item is in the BNRItemStore?


look at ItemsViewController.m , method didSelectRowAtIndexPath

When the user press a row on the tableview,first thing you do programmatically is to create an instance of the viewController you want to show and pass it the info, in this case the DetailViewController:

then, you created a pointer to the selected item (the item that was in the row pressed) when you did:

//you create an inmutable array here from the BNRITemStore's instance variable "allItems"
NSArray *items = [[BNRItemStore SharedStore] allItems];

//and here you create a new BNRItem pointer that points to the object in the array, this object is the "selected item"
//in other words, you create a pointer to and object that already exists.
 BNRItem *selectedItem = [items objectAtIndex:indexPath.row];

Remember you created a property in DetailViewController named Item?..you set that property to the selectedItem pointer in this line

you are playing with pointers, that means when you update the Item properties, you are updating the object who’s being pointed, in this case, the original object in the array, or the object int the selected row.(The object int the position IndexPath.row)

-(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear]; item.itemName = nameField.text; item.serialNumber = serialNumber.text; item.valueInDollars = valueField.text.intValue; item.dateCreated = [dateFormatter dateFromString:dateLabel.text]; }

that is the reason you have your item’s instance updated in the array but, in order to see the updated data you have to reload the data in the tableView.

Im sorry for my english since spanish is my native language.
Let me know if you undertand this.


Ok that makes sense. Thank you for the explanation!