我正在创建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的委托。

新类

#import ImplementationView.h
@interface NewClass : ImplementationView


新类

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


让我知道您是否需要任何其他帮助来澄清问题。任何帮助将不胜感激。提前致谢。

最佳答案

您的按钮不在layerTagView边界上。
您需要设置layerTagView框架/大小。在您的代码中,layerTagView的大小为(0,0)。
为了检查我的建议,您可以将masksToBounds设置为YES或将layerTagView的背景色更改为蓝色或任何其他颜色。

tagView.layer.masksToBounds = YES;
layerTagView.layer.masksToBounds = YES;

关于ios - UIButton没有响应我对collectionView单元的触摸吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32916847/

10-11 14:52