无法在 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");
}
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以,这就是答案。
我正在插入一个自定义子视图,其大小与整个屏幕 (320x640) 相同。它还附加了手势识别器。因此,每当我点击该视图时,gestureRecognizer 都会响应附加的子视图,而不是 uitoolbar 或其上的按钮。伙计,我很愚蠢。调整了 UIView 上的图像大小,一切正常。 :) 谢谢大家的帮助。
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.
initWithCustomView
- 视图必须是按钮并在按钮上设置操作initWithCustomView
- view must be button and set action on button