无法在UIBarButtonItem上设置操作 [英] Cannot set action on UIBarButtonItem

查看:112
本文介绍了无法在UIBarButtonItem上设置操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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文件:

#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");    
}


推荐答案

所以,这就是答案。

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

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.

这篇关于无法在UIBarButtonItem上设置操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆