繁体   English   中英

UIButton没有在自定义UITableViewCell中获取触摸事件

[英]UIButton not getting touch events inside a custom UITableViewCell

我在IB中设计了一个自定义的UItableView Cell。 该单元格具有UIButton,并具有相关的操作。 此按钮未获取触摸事件,但是单元格本身在调用时获取事件。

我在这里可能做错了什么。

+--------------------------------+
|                     +----------+
|  Cell               | Button   |
|                     +----------+
+--------------------------------+

当我点击Button tableviewcell获取事件时。 可能有什么不对?

编辑:

我刚刚检查过,UIButton也正在接触触摸事件,但是UITableViewCell也是如此。 然后在结束时,与UIButton相关的动作不会被调用。

我遇到过同样的问题。 最后注意到有问题的按钮在属性检查器中未选中“已启用用户交互”复选框。

确保您具有与此按钮关联的IBAction,用于状态UIControlEventTouchUpInside并启用用户交互。 然后文件所有者应该接收触摸事件。

这可能不正确,但是可以解决,

如果我在代码中动态创建按钮,应用程序工作正常。

customtablecell.h
你需要声明一个新的控制器,例如UIViewController *parentViewController;
做财产和合成
customtablecell.m文件中写下以下代码行

-(IBAction)myButtonAction:(id)sender {
 if (self.parentViewController) { 
        [self.parentViewController performSelector:@selector(myButtonClicked: forCellWithLabel:) withObject:sender withObject:[self.NameOfTrack text]];
    }
}

在customtablecell xib中连接这个-(IBAction)myButtonAction:(id)sender to button

在tableViewController中

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method
cell.parentViewController = self;





- (void)myButtonClicked:(id)sender forCellWithLabel:(NSString *)text {
    UIButton *button = (UIButton  *)sender;
  //Do some thing

}

暂无
暂无

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

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