其实我想将图像和标签作为UIBarButtonItem放置在工具栏上,并为该按钮提供可点击的效果。UIBarButtonItem目标方法不起作用
所以我在这里完成的是我创建了一个自定义视图,并将图像和标签放在同一个自定义视图上,最后将这个自定义视图作为UIBarButtonItem CustomView放置。
但是,当我设置相同的UIBarButtonItem目标和行动,它不是调用选择器方法。
整个代码如下。
有人可以建议我在我的代码中有什么错误吗?并有没有其他方法来实现相同的?
早期的建议将不胜感激。
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];
UIView *customView = [[UIView alloc]
initWithFrame:CGRectMake(0,0,self.navigationController.toolbar.frame.size.width,self.navigationController.toolbar.frame.size.height)];
customView.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:187.0/255.0 blue:150.0/255.0 alpha:1.0];
[self.navigationController.toolbar addSubview:customView];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(60,0,44,44)];
imgView.image = [UIImage imageNamed:@"documentImage.png"];
[customView addSubview:imgView];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(104,0,145,44)];
lbl.text = @"Scan Document";
lbl.textAlignment = NSTextAlignmentLeft;
[customView addSubview:lbl];
UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithCustomView:customView];
bar.target = self;
bar.action = @selector(scanDocument);
self.toolbarItems = [NSArray arrayWithObjects:bar, nil];
}
看到它它可以帮助你http://stackoverflow.com/questions/2796438/uibarbuttonitem-target-action-not-working –
你的scanDocument被定义为一个接受参数或不接受? - (void)scanDocument:(UIBarButtonItem *)sender'或者 - (void)scanDocument' –
No Parameter Just - (void)ScanDocument –