简体   繁体   English

UIButton touchUpInside事件无法正确触发

[英]UIButton touchUpInside event not firing correctly

I have a tableview that contains a row with a custom cell that contains a UIButton . 我有一个tableview,其中包含带有包含UIButton的自定义单元格的行。 However, the button doesn't always fire the action. 但是,按钮并不总是会触发动作。 Here's my code: 这是我的代码:

   submitButton = [[UIButton alloc] init];

    [[submitButton layer] setBorderColor:[[UIColor whiteColor] CGColor]];
    [submitButton setClipsToBounds: YES];
    submitButton.backgroundColor = [UIColor grayColor];
    [submitButton setTitle:@"Send" forState:UIControlStateNormal];
    [self.contentView addSubview:submitButton];
    [submitButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [submitButton release];

This is called in the custom cell's -(id)initWithStyle: 在自定义单元格的-(id)initWithStyle:调用此方法-(id)initWithStyle:

The buttonAction method looks like this: buttonAction方法如下所示:

 -(void)buttonAction
 {
     NSLog(@"Button Clicked!");
 }

It seems that the only way I can get the buttonAction to fire is if I press down on the button and release somewhere inside the cell's frame, but not inside the button itself. 看来,使buttonAction触发的唯一方法是按下按钮并释放单元格框架内的某个位置,而不是按钮本身内的某个位置。 Why would that be? 为什么会这样呢?

* UPDATE * Problem still exists, but I found that the more consistent way to get the button to fire is to click and drag to the left or right and then let go, as long as I let go within the bounds of the cell/row. * 更新 *问题仍然存在,但是我发现使按钮触发的更一致的方法是单击并向左或向右拖动,然后放开,只要我在单元格/行的范围内放开。

UPDATE #2 It looks like if I use iOS 6.0, it works as intended. 更新#2看起来如果我使用iOS 6.0,它可以按预期工作。 But on 5.0 or 5.1 it does not. 但是在5.0或5.1上则没有。

Try [submitButton sizeToFit] . 尝试[submitButton sizeToFit] I'm wondering whether your button has any size (since I don't see you giving it any). 我想知道您的按钮是否有任何尺寸(因为我看不到您给它任何尺寸)。

Also: Create your button with [UIButton buttonWithType: UIButtonTypeCustom] instead of alloc-init. 另外:使用[UIButton buttonWithType: UIButtonTypeCustom]而不是alloc-init创建按钮。

Try assigning the same method call to TouchUpOutside as well - you should then see it work every time. 尝试将相同的方法调用也分配给TouchUpOutside然后您应该会看到它每次都起作用。 TouchUpInside is only fired if you lift your finger while still within the bounds of the button. 仅当您仍在按钮范围内举起手指时,才会触发TouchUpInside。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM