如何以编程方式将触摸事件伪造为 UIButton?

how to programmatically fake a touch event to a UIButton?(如何以编程方式将触摸事件伪造为 UIButton?)
本文介绍了如何以编程方式将触摸事件伪造为 UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些单元测试,并且由于这个特定应用程序的性质,重要的是我要在 UI 链中尽可能高.所以,我想做的是以编程方式触发按钮按下,就好像用户按下了 GUI 中的按钮一样.

I'm writing some unit tests and, because of the nature of this particular app, it's important that I get as high up the UI chain as possible. So, what I'd like to do is programmatically trigger a button-press, as if the user had pressed the button in the GUI.

(是的,是的——我可以只调用 IBAction 选择器,但同样,这个特定应用程序的性质使得我假装实际按下按钮很重要,以便从按钮本身调用 IBAction.)

(Yes, yes -- I could just call the IBAction selector but, again, the nature of this particular app makes it important that I fake the actual button press, such that the IBAction be called from the button, itself.)

这样做的首选方法是什么?

What's the preferred method of doing this?

推荐答案

原来如此

[buttonObj sendActionsForControlEvents:UIControlEventTouchUpInside];

在这种情况下,我得到了我需要的东西.

got me exactly what I needed, in this case.

不要忘记在主线程中执行此操作,以获得类似于用户按下的结果.

Don't forget to do this in the main thread, to get results similar to a user-press.

对于 Swift 3:

buttonObj.sendActions(for: .touchUpInside)

这篇关于如何以编程方式将触摸事件伪造为 UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)