自定义按钮 touchUpInside 事件未触发

Custom Button touchUpInside event not fire

提问人:Vivek 提问时间:8/4/2017 更新时间:8/4/2017 访问量:318

问:

当我在自定义按钮中放置一些代码时,我的自定义按钮 touchupinside 事件没有触发

注意:当我点击按钮时,此方法是触发的,但touchUpInside方法未触发

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

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

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

iOS Objective-C iPhone iOS7 UIBubutton

评论

0赞 Anbu.Karthik 8/4/2017
touchesStarted 调用每个 UIcontrols,那么您将如何处理此问题
0赞 Vivek 8/4/2017
@Anbu.Karthik 请检查下面的答案,这对我来说会很好,所以我的问题已经解决了。

答:

1赞 mag_zbc 8/4/2017 #1

你忘了打电话super

[super touchesBegan:touches withEvent:event];

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

完整代码

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

评论

0赞 mag_zbc 8/4/2017
是的,因为您的自定义按钮使用其父函数。
0赞 Vivek 8/4/2017
我把代码放在哪里[super touchesBegan:touches withEvent:event];
0赞 mag_zbc 8/4/2017
在函数开始时
0赞 Vivek 8/4/2017
正如您所说,每个发送事件都放入此方法中?喜欢,等等......Value ChangeTouchUpInside
0赞 Arjun Singh Baghel 8/4/2017 #2

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

[super touchesBegan:touches withEvent:event];