无法在UIBarButtonItem上设置操作

时间:2011-12-04 06:08:39

标签: iphone ios xcode uibarbuttonitem

所以我在UIToolbar中添加了一堆自定义UIBarButtonItems,它们都加载得很好,看起来很棒,我可以在UIToolbar中看到它们。唯一的问题是,我无法对它们执行任何操作。在我的viewDidLoad中,我设置了所有内容。这是我的.h和.m文件:

.h文件:

#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文件:(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");    
}

2 个答案:

答案 0 :(得分:5)

所以,这就是答案。

我正在插入一个与整个屏幕大小相同的自定义子视图(320x640)。它还附有gestureRecognizers。因此,每当我点击该视图时,gestureRecognizer都会响应附加的子视图,而不是uitoolbar或其上的按钮。男人,我很愚蠢。调整UIView上的图像大小,一切正常。 :)感谢大家的帮助。

答案 1 :(得分:0)

initWithCustomView - 视图必须是按钮并在按钮上设置操作