简体   繁体   English

UIButton的事件touchUpInside不起作用

[英]the event touchUpInside of a UIButton don't work

there is only 1 cell in my UITableView, and I add a UIButton to the cell. 我的UITableView中只有一个单元格,我在单元格中添加了一个UIButton。 set the action when touchUpInside event happens. 在touchUpInside事件发生时设置操作。 But it doesn't work at all. 但它根本不起作用。 Meanwhile, if i set the trigger as touchDown event instead of touchUpInside, it will fire the action i set. 同时,如果我将触发器设置为touchDown事件而不是touchUpInside,它将触发我设置的动作。

I search the reason by google, someone says that the problem maybe caused by the superview's dimension. 我通过谷歌搜索原因,有人说这个问题可能是由superview的维度引起的。 That is the size of the button is beyond its superview. 这就是按钮的大小超出了它的超级视图。 so I add a UIView to contain the button, the code is as follow: 所以我添加一个UIView来包含按钮,代码如下:

self.ageDisplay = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
UIImage *ageImage = [UIImage imageNamed:@"long_btn_click.png"];
[self.ageDisplay setImage:ageImage forState:UIControlStateSelected];
[self.ageDisplay setFrame:CGRectMake(10, 5, 145, 34)];//(10, 320, 145, 25)];
[self.ageDisplay setTitle:@"请选择您的年龄" forState:UIControlStateNormal];
[self.ageDisplay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.ageDisplay setTag:3];
[self.ageDisplay addTarget:self action:@selector(showSelectionPage:) forControlEvents:UIControlEventTouchDown];

self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 310, 320, 106)];
//self.buttonsSubView.backgroundColor = [UIColor blueColor];
[self.buttonsSubView setUserInteractionEnabled:YES];
[self.buttonsSubView addSubview:self.ageDisplay];

the above code is in ViewDidLoad function. 上面的代码是在ViewDidLoad函数中。

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *sugPageCell = @"SuggestionPageCell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:sugPageCell];
    if (nil == cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sugPageCell];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    [cell addSubview:self.sugTitle];
    [cell addSubview:self.sugSpecification];
    [cell addSubview:self.suggestion];
    self.buttonsSubView.userInteractionEnabled = YES;
    [cell addSubview:self.buttonsSubView];

    /*
    [cell addSubview:self.ageDisplay];
    [cell addSubview:self.genderDisplay];
    [cell addSubview:self.submit];
     */

    return cell;

}

However, this doesn't help. 但是,这没有用。 The problem has confused me for 2 days. 这个问题困扰了我2天。 I will appreciate if anyone can provide some indication. 如果有人能提供一些指示,我将不胜感激。 Thank you. 谢谢。

It's because your buttonSubView view is outside the cell's frame. 这是因为你的buttonSubView视图在单元格的框架之外。 So even if the button is visible (because the cell's clipsToBounds is set to NO), it will not receive touch events from the cell. 因此,即使按钮可见(因为单元格的clipsToBounds设置为NO),它也不会从单元格接收触摸事件。 Why do you set this view's vertical position to 310 ? 为什么将此视图的垂直位置设置为310?

If you try this : 如果你试试这个:

self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 106)];

The event should fire as you want. 该活动应该随心所欲。

I had this problem recently. 我最近遇到了这个问题。 I guess there must may be an object of UITapGesture added to your UITableView . 我猜UITapGesture的对象可能会添加到您的UITableView If so, please set the property cancelsTouchesInView of the UITapGesture object to NO. 如果是这样,请将UITapGesture对象的属性cancelsTouchesInView设置为NO。

For example: 例如:

 UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:tapAction];
gesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:gesture];

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

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