Changes in viewWillAppear?


#1

I’m looking for some help to see if I missed a part of the book (paper, not e).

I just finished ch.23 and on pg 449 we need to add some code to BNRDetailViewController.m in the viewWillAppear method. A section of code is shown in regular type font as being the pre-existing code. My problem is that my code doesn’t look like what the book says should be there. Here’s what the book says this method should look like BEFORE we make the changes on pg 449:

BNRDetailViewController:viewWillAppear()

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

UIInterfaceOrientation io = [[UIApplication sharedApplication] statusBarOrientation];
[self prepareViewsForOrientation:io];

BNRItem *item = self.item;

self.nameField.text = item.itemName;
self.serialNumberField.text = item.serialNumber;
self.valueField.text = [NSString stringWithFormat:@"%d", item.valueInDollars];

// You need a NSDateFormatter that will turn a date into a simple date string
static NSDateFormatter *dateFormatter;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
}

// Use filtered NSDate object to set dateLabel contents
self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];

NSString *itemKey = self.item.itemKey;
if (itemKey) {
    // Get image for image key from the image store
    UIImage *imageToDisplay = [[BNRImageStore sharedStore] imageForKey:itemKey];

    // Use that image to put on the screen in imageView
    self.imageView.image = imageToDisplay;
} else {
    // Clear the imageView
    self.imageView.image = nil;
}

[self updateFonts];

[/code]

I’ve searched thru the downloaded solution code and it looks like the authors changed this method in ch. 15. When I look thru the book the last time I can find that we changed this method is in ch. 11, pg 228.

This is what my viewWillAppear method looks like:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear];
    
    UIInterfaceOrientation io = [[UIApplication sharedApplication] statusBarOrientation];
    
    [self prepareViewsForOrientation:io];
    
    //Add the BNRItem that will be displayed in the detail
    BNRItem *item = self.item;
    
    self.nameField.text = item.itemName;
    self.serialNumberField.text = item.serialNumber;
    self.valueField.text = [NSString stringWithFormat:@"%d", item.valueInDollars];
    
    //We need an NSDateFormatter that will turn a date into a simple date string
    static NSDateFormatter *dateFormatter = nil;
    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateStyle = NSDateFormatterMediumStyle;
        dateFormatter.timeStyle = NSDateFormatterNoStyle;
    }
    //Use filtered NSDate object to set dateLabel contents
    self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];
    
    NSString *imageKey = self.item.itemKey;
    
    //Get the image for its image key from the image store
    UIImage *imageToDisplay = [[BNRImageStore sharedStore] imageForKey];
    
    //Use that image to put on the screen in the imageView
    self.imageView.image = imageToDisplay;

    [self UpdateFonts];
    
}

The difference between the two is this code snippet:

[code]NSString *itemKey = self.item.itemKey;
if (itemKey) {
// Get image for image key from the image store
UIImage *imageToDisplay = [[BNRImageStore sharedStore] imageForKey:itemKey];

    // Use that image to put on the screen in imageView
    self.imageView.image = imageToDisplay;
} else {
    // Clear the imageView
    self.imageView.image = nil;
}[/code]

According to the downloaded source code, the change to this method happened after ch.11 and before ch. 15. I can’t find that change in the book but it’s possible I completely missed it. The fact that nobody has mentioned it here makes me think I missed it. My code ‘seems’ to work ok, I don’t get any errors and it works as expected. Any help would be appreciated. Thanks

Frank


#2

Hi, yes this was an issue with the first printing of the 4th edition. We’ve fixed this error with the second printing that is currently for sale.

Looking at this code:

    NSString *itemKey = self.item.itemKey;
    if (itemKey) {
        // Get image for image key from the image store
        UIImage *imageToDisplay = [[BNRImageStore sharedStore] imageForKey:itemKey];

        // Use that image to put on the screen in imageView
        self.imageView.image = imageToDisplay;
    } else {
        // Clear the imageView
        self.imageView.image = nil;
    }

BNRItems always have an itemKey, so the code to grab the imageToDisplay and setting that on the imageView will always get run (the else block will never get hit). In earlier editions of the book, BNRItems only had an “itemKey” (called an imageKey at the time) when there was an image associated with the item. For that reason, we had the conditional logic that I’ve quoted above.

Sorry for the confusion. Hope that helps!


#3

Thanks for the quick reply, Christian. On a side note, I’m really enjoying working my way thru the book (I purchased the BNR Obj-C book, also, and did that book in about 2 weeks).