IOS UIButton Target不起作用

时间:2014-06-25 09:14:51

标签: ios uiscrollview uibutton

这是我的代码

   for (NSString *message in self.messages) {
    label = [[UIButton alloc] initWithFrame:CGRectZero];
    [label setTitle:message forState:UIControlStateNormal];
    //label.titleLabel.text = message;
    label.tag = i;
    [label addTarget:self action:@selector(gototest:) forControlEvents:UIControlEventTouchDown];

            CGSize size = [message sizeWithFont:label.titleLabel.font];
    CGFloat width = size.width + kPADDING;
    label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
    [self addSubview:label];

    i++;

    xPos += width;
   }

-(void)gototest:(UIButton*)sender
{
int tag = sender.tag;
NSLog(@"test %@",@"ccc ");
}

点击按钮时没有任何反应。

请注意,该类是从UIScrollView扩展而且我必须创建一个包含不同新闻的字幕,当我点击任何此新闻时,我必须转到所选新内容的详细信息。

请帮帮我

5 个答案:

答案 0 :(得分:0)

使用触摸事件" UIControlEventTouchUpInside"打算" UIControlEventTouchDown"。

答案 1 :(得分:0)

它会正常工作:

  

[label addTarget:self action:@selector(grottiest :)   forControlEvents:UIControlEventTouchUpInside];

编辑:

  label = [[UIButton alloc] initWithFrame:CGRectMake(xPos, 0.0, 100, 100)];
  [label setTitle:message forState:UIControlStateNormal];
  //label.titleLabel.text = message;
  label.tag = i;
  [label addTarget:self action:@selector(gototest:) forControlEvents:UIControlEventTouchUpInside];
  [self addSubview:label];

答案 2 :(得分:0)

UIControlEventTouchDown应该不是问题,它也有效。你能尝试下面的

吗?
label = [UIButton buttonWithType:UIButtonTypeCustom];

而不是

label = [[UIButton alloc] initWithFrame:CGRectZero];

我也怀疑以下一行

label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);

您正在设置的帧确认它是正确的,在正常使用情况下,您的UIButton可能会显示,但可能不会。试试吧

label.clipsToBounds=YES;

并检查,如果你的UIButton实际上是可见的,那可能是你的行动不起作用的原因..

<强>更新

好的,根据我的理解,正如您所说,您正在尝试创建Marquee,原始实现已UILabel,您正尝试将其更改为UIButton。让我们改变一些事情,如下面的代码,

for (NSString *message in self.messages) {
    label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = message;
    label.tag = i;

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gototest:)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [label addGestureRecognizer:tapGestureRecognizer];
    label.userInteractionEnabled = YES;



    CGSize size = [message sizeWithFont:label.font];
    CGFloat width = size.width + kPADDING;
    label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
    [self addSubview:label];

    i++;

    xPos += width;
}

你的行动

-(void)gototest:(UITapGestureRecognizer*)sender
{
   int tag = sender.view.tag;
   NSLog(@"test %@",@"ccc ");
}

答案 3 :(得分:0)

您的代码使用单个按钮正常工作。问题出在其他地方..

    UIButton *label = [[UIButton alloc] initWithFrame:CGRectZero];
    [label setTitle:@"Click" forState:UIControlStateNormal];
    //label.titleLabel.text = message;
    label.tag = 0;
    [label addTarget:self action:@selector(gototest:) forControlEvents:UIControlEventTouchDown];

    label.frame = CGRectMake(0,0,100,50);
    [self addSubview:label];

答案 4 :(得分:0)

替换:

 label = [[UIButton alloc] initWithFrame:CGRectZero];

UIButton *label = [[UIButton alloc] initWithFrame:CGRectZero];