Silver Challenge Solution - With Some Extras


#1

For the silver challenge I deiced to use the toolbar to create a delete mechanism rather than adding a button to the screen. I was not sure how to append a button in code to a toolbar button created in IB so instead I created all the buttons for the toolbar in code (i.e. including the camera). Also the instructions said to clear the image which I assumed also meant to delete it from the store.

All code was done in BNRDetailViewController.m

Added 1 property to the local interface that is an array holding the buttons that will be displayed in the toolbar.

...
@property (strong,nonatomic) NSMutableArray *buttonArray;
...

Here I am creating the buttons to be put onto the toolbar. Again, I deleted the button created in IB and instead created it in code. Additionally, the delete icon should only show if there is an image to delete. I handle that via the conditional statement. As a note I also created a “buttonSpacer” so that the camera button and the delete button would not be next to each other. This is for no other reason than visual preference.

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear];
    
    BNRItem *item = self.item;
    
    self.nameField.text = item.itemName;
    self.serialNumberField.text = item.serialNumber;
    self.valueField.text = [NSString stringWithFormat:@"%d", item.valueInDollars];
    
    static NSDateFormatter *dateFormatter = nil;
    if (!dateFormatter)
    {
        dateFormatter = [[NSDateFormatter alloc]init];
        dateFormatter.dateStyle = NSDateFormatterMediumStyle;
        dateFormatter.timeStyle = NSDateFormatterNoStyle;
    }
    
    self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];
    
    
    NSString *imageKey = self.item.itemKey;
    
    UIImage *imageToDisplay = [[BNRImageStore sharedStore]imageForKey];
    
    self.imageView.image = imageToDisplay;
    
    UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
                                                                                 target:self
                                                                                 action:@selector(takePicture)];
    
    UIBarButtonItem *buttonSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                 target:self
                                                                                 action:nil];
    
    self.buttonArray = [[NSMutableArray alloc]initWithObjects:cameraButton,buttonSpacer,nil];
    
    if (imageToDisplay)
    {
        UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
                                                                         target:self
                                                                         action:@selector(removeImage)];
        
        [self.buttonArray insertObject:deleteButton atIndex:2];

    }
    
    [self.toolbar setItems:self.buttonArray];
}

Lastly, when the user clicks the trashcan to delete this method fires. It clears the image from the screen, deletes the image association from the store, and updates the toolbar to essentially remove the trashcan icon as now there is nothing for the user to delete.

-(void)removeImage
{
    self.imageView.image = nil;
    
    NSString *imageKey = self.item.itemKey;
    
    [[BNRImageStore sharedStore]deleteImageForKey];
    
    [self.buttonArray removeObjectAtIndex:2];
    
    [self.toolbar setItems:self.buttonArray];
}

One final thing to add. Since I created the buttons in code I changed the takePicture method to return void and take no arguments.

- (void)takePicture
...

#2

In my case, I added a bar button labeled ‘Del Image’ to Toolbar, next to camera button in xib file. And then I utilized its ‘enabled’ attribute to disable the button when there is no image specified.

Yours seems better with hidden button with no image set.


@property (weak, nonatomic) IBOutlet UIBarButtonItem *delImageButton;

- (IBAction)deleteImage:(UIBarButtonItem *)sender
{
    self.imageView.image = nil;
    [[BNRImageStore sharedStore] deleteImageForKey:self.item.itemKey];
    self.delImageButton.enabled = NO;
}

-(void)viewWillAppear:(BOOL)animated
    if (imageToDisplay) {
        self.delImageButton.enabled = YES;
    } else {
        self.delImageButton.enabled = NO;
    }
    ... ...