how to programmatically fake a touch event to a 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以编程方式将触摸事件伪造为 UIButton?


- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- UITextView 内容插图 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01