Reputation: 661
So I'm adding a bunch of custom UIBarButtonItems to a UIToolbar, and they all load just fine and look great, and I can see them in my UIToolbar. The only problem is, I can't perform any actions on them. In my viewDidLoad, I set up everything. Here are my .h and .m files:
.h file:
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <QuartzCore/QuartzCore.h>
@interface PhotoEditViewController : UIViewController <UIPopoverControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIGestureRecognizerDelegate> {
IBOutlet UIImageView *imageView;
}
- (IBAction)cancelEdit:(id)sender;
- (IBAction)savePhoto:(id)sender;
- (IBAction)chooseColor:(id)sender;
@property (retain) IBOutlet UIToolbar *editBar;
@property (retain) UIImageView *imageView;
@property (retain) UIImage *tempImage;
- (void) setupStache;
@end
.m file: (viewDidLoad)
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView.image = tempImage;
NSMutableArray *toolbarItems = [NSMutableArray arrayWithCapacity:3];
// get the filepaths of all the images
NSString *imagePath_cancel = [[NSBundle mainBundle] pathForResource:@"barBtnPhotoEditCancel" ofType:@"png"];
NSString *imagePath_save = [[NSBundle mainBundle] pathForResource:@"barBtnSavePhoto" ofType:@"png"];
NSString *imagePath_color = [[NSBundle mainBundle] pathForResource:@"barBtnChangeColor" ofType:@"png"];
// get the images
UIImage *cancelImage = [[UIImage alloc] initWithContentsOfFile:imagePath_cancel];
UIImage *saveImage = [[UIImage alloc] initWithContentsOfFile:imagePath_save];
UIImage *changeColorImage = [[UIImage alloc] initWithContentsOfFile:imagePath_color];
// set the images to the UIBarButtonItem(s)
CGRect cancelFrame = CGRectMake(0, 0, cancelImage.size.width-25, cancelImage.size.height-25);
UIButton* cancelButton = [[UIButton alloc] initWithFrame:cancelFrame];
[cancelButton setBackgroundImage:cancelImage forState:UIControlStateNormal];
[cancelButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
[cancelBarButtonItem setTarget:self];
[cancelBarButtonItem setAction:@selector(cancelEdit:)];
CGRect saveFrame = CGRectMake(0, 0, saveImage.size.width-25, saveImage.size.height-25);
UIButton* saveButton = [[UIButton alloc] initWithFrame:saveFrame];
[saveButton setBackgroundImage:saveImage forState:UIControlStateNormal];
[saveButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem* saveBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveButton];
[saveBarButtonItem setTarget:self];
[saveBarButtonItem setAction:@selector(savePhoto:)];
CGRect colorFrame = CGRectMake(0, 0, changeColorImage.size.width-25, changeColorImage.size.height-25);
UIButton* colorButton = [[UIButton alloc] initWithFrame:colorFrame];
[colorButton setBackgroundImage:changeColorImage forState:UIControlStateNormal];
[colorButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem* colorBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:colorButton];
[colorBarButtonItem setTarget:self];
[colorBarButtonItem setAction:@selector(chooseColor:)];
// add all the items
[toolbarItems addObject:cancelBarButtonItem];
[toolbarItems addObject:saveBarButtonItem];
[toolbarItems addObject:colorBarButtonItem];
[self.editBar setItems:toolbarItems animated:NO];
// release everything
[cancelBarButtonItem release];
[cancelButton release];
[saveBarButtonItem release];
[saveButton release];
[colorBarButtonItem release];
[colorButton release];
}
- (IBAction)cancelEdit:(id)sender {
NSLog(@"Pressing the cancel button");
}
- (IBAction)savePhoto:(id)sender {
NSLog(@"Pressing the save button");
}
- (IBAction)chooseColor:(id)sender {
NSLog(@"Pressing the choose color button");
}
Upvotes: 1
Views: 3376
Reputation: 661
So, here's the answer.
I was inserting a custom subview that was the size of the entire screen (320x640). It also had gestureRecognizers attached to it. Therefore, whenever I tapped that view, the gestureRecognizer responded to the attached subview, instead of the uitoolbar, or the buttons on it. Man I'm foolish. Resized the image on the UIView, and everything works fine. :) Thanks everyone for the help.
Upvotes: 5
Reputation: 3046
initWithCustomView
- view must be button and set action on button
Upvotes: 0