我正在创建collectionView,并且在每个单元格中我都在水平创建几个按钮,但这些按钮没有响应我的目标选择器。
这是我的代码。
ImplementationView.h
-(IBAction)tagClicked:(UIButton*)sender;
在ImplementationView.m
中-(void)configureScrapCell:(UICollectionViewCell*) cell forScrapDict:(ScrapDictionary *)dict forIndexPath:(NSIndexPath*){
UIView *tagView = (UIView*)[cell viewWithTag:17];
UIView *layerTagView = (UIView*)[cell viewWithTag:18];
CGFloat x = 0.0;
if (layerTagView) {
[layerTagView removeFromSuperview];
}
layerTagView = [[UIView alloc] init];
layerTagView.tag = 18;
[tagView addSubview:layerTagView];
if ([dict.SDtags count]>0) {
int tagIndex = 1;
for (NSString *tag in dict.SDtags) {
CGSize width = [tag sizeWithFont:[UIFont systemFontOfSize:16]];
UIButton *scrapTag = [[UIButton alloc]initWithFrame:CGRectMake(x, 0,100, 30)];
[scrapTag setBackgroundColor:[UIColor greenColor]];
[scrapTag.titleLabel setFont:[UIFont systemFontOfSize:14]];
// [scrapTag setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
if (brightness > 150) {
[scrapTag setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}else {
[scrapTag setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
[scrapTag setTitle:[NSString stringWithFormat:@" #%@",tag] forState:UIControlStateNormal];
// [scrapTag sizeToFit];
[scrapTag setUserInteractionEnabled:YES];
tagIndex++;
NSString *tagString = @"";
tagString = [tagString stringByAppendingString:tag];
if ([tagString length]>32 || tagIndex>4) {
break;
}
[scrapTag addTarget:self action:@selector(tagClicked:) forControlEvents:UIControlEventTouchUpInside];
[layerTagView addSubview:scrapTag];
NSLog(@"%f",scrapTag.frame.size.width);
// scrapTag.frame = CGRectMake(x, scrapTag.frame.origin.y, tagView.frame.size.width, 30);
x += scrapTag.frame.size.width +2;
}
}
[tagView setBackgroundColor:[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:0.5]];
[layerTagView setBackgroundColor:[UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:0.5]];
现在在我正在定义这些按钮Clicked的动作的不同类中。 ImplementationView类充当NewClass的委托。
NewClass.h
#import ImplementationView.h
@interface NewClass : ImplementationView
NewClass.m
-(IBAction)tagClicked:(UIButton *)sender {
[[[UIAlertView alloc]initWithTitle:@"LOL" message:[NSString stringWithFormat:@"%@",sender.titleLabel.text] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]show ];
NSLog(@"testing tag clicked");
}
如果您需要任何其他帮助以明确问题,请与我们联系。任何帮助将非常感激。提前谢谢。
答案 0 :(得分:1)
您的按钮不在layerTagView边界上。 您需要设置layerTagView框架/大小。在您的代码中,layerTagView的大小为(0,0)。 要检查我的建议,您可以将masksToBounds设置为YES或将layerTagView的背景颜色更改为蓝色或任何其他颜色。
tagView.layer.masksToBounds = YES;
layerTagView.layer.masksToBounds = YES;