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?
基础教程推荐
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
