Challenge "Removing an Image" with UIBarButtonSystemItems


#1

Hi there,

i wanted to add the standard “trash” button next to the camera button if a possession has an image.
here is my solution

  1. For using multiple UIBarButtonSystemItems, we have to use a UIToolbar as a view for the RightBarButtonItem of the navigation bar.
    The toolbar has to be created, if a possession has an image. For this add following code into the viewWillAppear-method from the ItemDetailViewController
-(void)viewWillAppear:(BOOL)animated
{
	(...)

	if (imageKey) {
		
                (...)

		//Create a toolbar to have two buttons in the right
		UIToolbar *toolBarItems = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 92, 44.01)];
		
		//Create the array to hold the buttons, which the gets added to the toolbar
		NSMutableArray *toolBarButtons =[[NSMutableArray alloc] initWithCapacity:3];
		
		//Create the standard "trash" button
		UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
																				target:self 
																				action:@selector(deletePicture:)];
		[button setStyle:UIBarButtonItemStyleBordered];
		[toolBarButtons addObject:button];
		[button release];
		
		//Create a spacer
		button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
															   target:nil 
															   action:nil];
		[toolBarButtons addObject:button];
		[button release];
		
		//Create the standard "camera" button
		button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera 
															   target:self 
															   action:@selector(takePicture:)];
		
		[button setStyle:UIBarButtonItemStyleBordered];
		[toolBarButtons addObject:button];
		[button release];	
		
		//Stick the buttons in the toolbar
		[toolBarItems setItems:toolBarButtons animated:NO];
		[toolBarButtons release];
		
		button = [[UIBarButtonItem alloc] initWithCustomView: toolBarItems];
		[toolBarItems release];
		//and put the toolbar in the nav bar
		[[self navigationItem] setRightBarButtonItem:button];
		[button release];
		
		
	}
        (...)
  1. The trash button should only appear for the possession with an image, therefore we have to set the RightBarButton of the nav bar for the possession without an image back to to
    initialized state.
-(void)viewWillAppear:(BOOL)animated
{
	(...)
	if (imageKey) {
                (...)
       }
       else {
		(...)

		//Create a UIBarButton with a camera icon, will send
		//takePicture: to our ItemDetailViewController when tapped
		UIBarButtonItem *cameraBarButtonItem =
		[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera 
													  target:self
													  action:@selector(takePicture:)];
		
		//Place this image on our navigation bar when this viewcontroller
		//is on top of the navogation stack
		[[self navigationItem] setRightBarButtonItem:cameraBarButtonItem];
		
		//cameraBarButton is retained by the navigation item
		[cameraBarButtonItem release];		
	}
}
  1. The code for deleting the image and again set the RightBarButton back to the standard one
-(void)deletePicture:(id)sender
{
	NSLog(@"Delete Picture\n");
	
	NSString *oldKey = [editingPossesion imageKey];
	//Delete the old image
	[[ImageCache sharedImageCache] deleteImageForKey:oldKey];
	//Clear the image view
	[imageView setImage:nil];
	//Create a UIBarButton with a camera icon, will send
	//takePicture: to our ItemDetailViewController when tapped
	UIBarButtonItem *cameraBarButtonItem =
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera 
												  target:self
												  action:@selector(takePicture:)];
	
	//Place this image on our navigation bar when this viewcontroller
	//is on top of the navogation stack
	[[self navigationItem] setRightBarButtonItem:cameraBarButtonItem];
	
	//cameraBarButton is retained by the navigation item
	[cameraBarButtonItem release];
	
}

This is my solution.
If you see any memory leaks or something else, tell them to me.
Hope this solution helps someone.


#2

Hi there again,

I found one mistake by myself.

If you deleted the image and go back to the table and then again looks for a detailed View of the possession the “trash” button is still there.
This was because i forgot to set the imageKey of the editingPossession to NULL
So you have to add following line in the deletePicture-methode

	[editingPossesion setImageKey:NULL];