【问题标题】:UIButton not getting touch events inside a custom UITableViewCellUIButton 没有在自定义 UITableViewCell 中获取触摸事件
【发布时间】:2010-12-24 19:31:26
【问题描述】:

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

我在这里做错了什么。

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

当我点击按钮时,tableviewcell 会获取事件。有什么问题?

编辑:

我刚刚检查过,UIButton 也获得了触摸事件,但 UITableViewCell 也是如此。最后,与 UIButton 关联的操作不会被调用。

【问题讨论】:

    标签: iphone ipad ios4


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        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 连接到按钮

        在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
        
        }
        

        【讨论】:

          【解决方案4】:

          确保您有一个与状态 UIControlEventTouchUpInside 的此按钮关联的 IBAction,并且用户交互已启用。然后文件所有者应该会收到触摸事件。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-19
            相关资源
            最近更新 更多