【问题标题】:Custom Button touchUpInside event not fire自定义按钮 touchUpInside 事件未触发
【发布时间】:2018-01-12 08:41:22
【问题描述】:

当我将一些代码放入自定义按钮时,我的自定义按钮 touchupinside 事件不会触发

注意:当我点击按钮时,此方法会触发,但 touchUpInside 方法不会触发

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchBegan");
    [Singleton playSoundForEvent:Sound_ButtonTap];
}

如果我删除上面的代码,那么工作正常。

以上代码原因:我不会在 touchupinside 上到处放敲击声。 我只想要整个项目中的一行代码。

【问题讨论】:

  • touchesBegan 调用每个 UIcontrols 那么你将如何处理这个
  • @Anbu.Karthik 请检查下面的答案,它对我来说会很好,所以我的问题已经解决了。

标签: ios objective-c iphone ios7 uibutton


【解决方案1】:

你忘了给super的电话

[super touchesBegan:touches withEvent:event];

没有调用 super 就无法工作的原因是按钮需要注册触摸事件(touchesBegantouchesEndedtouchesMovedtouchesCanceled)才能将它们映射到 @ 987654327@,然后才使用sendAction 触发操作。

完整代码

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    NSLog(@"touchBegan");
    [Singleton playSoundForEvent:Sound_ButtonTap];
}

【讨论】:

  • 是的,因为你的自定义按钮使用它的父函数。
  • 我在哪里放代码[super touchesBegan:touches withEvent:event];
  • 函数的开头
  • 正如你所说的每个发送事件都放入这个方法?比如Value ChangeTouchUpInside等等……
【解决方案2】:

请检查此代码并再次测试,希望它能正常工作。

[super touchesBegan:touches withEvent:event];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2012-07-18
    • 2023-01-03
    • 2019-09-09
    • 1970-01-01
    相关资源
    最近更新 更多