Where is this setItem: coming from?


#1

On page 207 it says “Instead, the BNRDetailViewController will set its title when it sets its item property.” I can’t find this setter method anywhere in the BNRItem and I have no idea how it is being overwritten. Where is this method implemented?


#2

It’s in BNRDetailViewController. It’s the setter for the item property.


#3

Thanks for the reply. This is where it is implemented but what I don’t know is how setItem: is being called. Is this an Apple superclass method that I don’t know about? I know some things get called in the background from Apple’s work like touchesDidBegin: and such and some need to be called by you like init. How is setItem: called?


#4

Sorry, I didn’t have the book earlier to refer to. On page 204 of the printed book (in Passing data between view controllers), there’s this very important line of code in tableView:didSelectRowAtIndexPath::

detailViewController.item = selectedItem;

This is what you are looking for. Remember that is exactly the same as:

[detailViewController setItem:selectedItem];

#5

Ahh I see, thanks so much.


#6

I am also confused about this. I understand that a new item is being created to hold whatever values the user enters into the text fields but I don’t understand how that item is then returned and set as the item in the original BNRItemStore array.


#7

A new item isn’t being created in the BNRDetailViewController. The item already exists in the store. When you tap on a row, the BNRItemsViewController grabs the relevant BNRItem and passes it to the BNRDetailViewController via it’s item property, as mentioned earlier. So when you are updating the item in BNRDetailViewController, you are actually updating the item that exists in the BNRItemStore.

Does that help?


#8

Hi Christian,

Thank you for your reply.

I definitely follow the logic that BNRItemsViewController passes the value onto BNRDetailViewControlller. My issue is with the following method in BNRDetailViewController:

[code]-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear];

// clear first responder
[self.view endEditing:YES];

// 'save' the changes to the item
BNRItem *item = self.item;
item.itemName = self.nameField.text;
item.valueInDollars = [self.valueField.text intValue];
item.serialNumber = self.serialNumberField.text;

}[/code]

Specifically my confusion is with the section that ‘saves’ the value of the text fields. Why do we create a new instance of BNRItem? We never return this item at the end of the method. It seems to me that this item is created and assigned the value of all of the text fields but nothing is ever done with it? I obviously understand that somewhere this newly created item’s values are returned but I can’t seem to follow what happens to the new instance of BNRItem created below the “// ‘save’ changes to the item” section.


#9

[quote=“clicker191”]Hi Christian,

Thank you for your reply.

I definitely follow the logic that BNRItemsViewController passes the value onto BNRDetailViewControlller. My issue is with the following method in BNRDetailViewController:

[code]-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear];

// clear first responder
[self.view endEditing:YES];

// 'save' the changes to the item
BNRItem *item = self.item;
item.itemName = self.nameField.text;
item.valueInDollars = [self.valueField.text intValue];
item.serialNumber = self.serialNumberField.text;

}[/code]

Specifically my confusion is with the section that ‘saves’ the value of the text fields. Why do we create a new instance of BNRItem? We never return this item at the end of the method. It seems to me that this item is created and assigned the value of all of the text fields but nothing is ever done with it? I obviously understand that somewhere this newly created item’s values are returned but I can’t seem to follow what happens to the new instance of BNRItem created below the “// ‘save’ changes to the item” section.[/quote]

A new BNRItem is not being created; I think that is the confusion. self.item is a BNRItem that is contained in the BNRItemStore, so there are two references to it. The BNRItemStore holds onto the BNRItem in its array of items, and the BNRDetailViewController has a reference to the same item through via self.item. So when we ‘save’ the values into self.item, the store knows about these changes since it is the same item.

I drew a picture of the model graph. I’m not sure if it’ll help, but take a look: cl.ly/image/1M3y1B412721 In the meantime, I’m going to try to think of an analogy to help make this more clear. But please let me know if this helps at all.


#10

Aha! Excellent. I’m with you now. After some repeated head banging your last reply made it click perfectly. Thanks Christian.


#11

[quote=“ChristianKeur”][quote=“clicker191”]Hi Christian,

Thank you for your reply.

I definitely follow the logic that BNRItemsViewController passes the value onto BNRDetailViewControlller. My issue is with the following method in BNRDetailViewController:

I drew a picture of the model graph. I’m not sure if it’ll help, but take a look: cl.ly/image/1M3y1B412721 In the meantime, I’m going to try to think of an analogy to help make this more clear. But please let me know if this helps at all.[/quote][/quote]
That diagram would be a great addition to the 5th Edition!


#12

Thanks Christian, I was dumbfounded by this also and forgot that we are just setting the pointer to the same copy when we are giving the detail view controller a pointer to the item object in row, not actually creating a new object. This was a good refresher on pointers. Copy and paste the explanation above into the 5th edition! :smiley:


#13

[quote=“ChristianKeur”][quote=“clicker191”]Hi Christian,

Thank you for your reply.

I definitely follow the logic that BNRItemsViewController passes the value onto BNRDetailViewControlller. My issue is with the following method in BNRDetailViewController:

[code]-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear];

// clear first responder
[self.view endEditing:YES];

// 'save' the changes to the item
BNRItem *item = self.item;
item.itemName = self.nameField.text;
item.valueInDollars = [self.valueField.text intValue];
item.serialNumber = self.serialNumberField.text;

}[/code]

Specifically my confusion is with the section that ‘saves’ the value of the text fields. Why do we create a new instance of BNRItem? We never return this item at the end of the method. It seems to me that this item is created and assigned the value of all of the text fields but nothing is ever done with it? I obviously understand that somewhere this newly created item’s values are returned but I can’t seem to follow what happens to the new instance of BNRItem created below the “// ‘save’ changes to the item” section.[/quote]

A new BNRItem is not being created; I think that is the confusion. self.item is a BNRItem that is contained in the BNRItemStore, so there are two references to it. The BNRItemStore holds onto the BNRItem in its array of items, and the BNRDetailViewController has a reference to the same item through via self.item. So when we ‘save’ the values into self.item, the store knows about these changes since it is the same item.

I drew a picture of the model graph. I’m not sure if it’ll help, but take a look: cl.ly/image/1M3y1B412721 In the meantime, I’m going to try to think of an analogy to help make this more clear. But please let me know if this helps at all.[/quote]

Christian,

I am trying to understand how the BNRItemStore is being updated.

NSArray *items = [[BNRItemStore sharedStore]allItems];
BNRItem *selectedItem = items[indexPath.row];

//Give detail view controller a pointer to the item object in row
detailViewController.item = selectedItem;

Clearly, we are passing the pointer that points to the same item as what was selected in the tableView. I now see how that pointer is being passed into the detail view controller . My issue is with the allItems method which returns a copy of the actual BNRItemStore array. So, doesn’t this pointer point to an item outside of the actual BNRItemStore array (and thus, never implements its changes in the important array?) Doesn’t that mean that the changes made to this particular item would not be made to that same item in the BNRItemStore? Does the static nature of BNRItemStore have something to do with this?

Thanks