Oli*_*lie 181 iphone uibutton touch-event ios
我正在编写一些单元测试,由于这个特定应用程序的性质,重要的是我尽可能地提高UI链条.所以,我想要做的是以编程方式触发按钮按下,就像用户按下按钮一样GUI.
(是的,是的 - 我可以调用IBAction选择器,但是,再次,这个特定的应用程序的性质使我很重要的是我伪造实际的按钮按下,这样就IBAction可以从按钮调用它本身.)
这样做的首选方法是什么?
Oli*_*lie 452
事实证明
[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我得到了我需要的东西.
编辑:不要忘记在主线程中执行此操作,以获得类似于用户按下的结果.
对于Swift 3:
buttonObj.sendActions(for: .touchUpInside)
Hel*_*ffy 50
对Swift的这个答案的更新
buttonObj.sendActionsForControlEvents(.TouchUpInside)
Run Code Online (Sandbox Code Playgroud)
编辑:更新为Swift 3
buttonObj.sendActions(for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)
如果你想进行这种测试,你会喜欢iOS 4中的UI自动化支持.你可以很容易地编写JavaScript来模拟按键等,尽管文档(特别是入门部分)有点疏.
在这种情况下,UIButton是源于UIControl.这适用于派生自的对象UIControl.
我想UIBarButtonItem在特定用例上重用" "操作.在这里,UIBarButtonItem不提供方法sendActionsForControlEvents:
但幸运的是,它UIBarButtonItem具有目标和行动的属性.
if(notHappy){
SEL exit = self.navigationItem.rightBarButtonItem.action;
id world = self.navigationItem.rightBarButtonItem.target;
[world performSelector:exit];
}
Run Code Online (Sandbox Code Playgroud)
这里rightBarButtonItem是类型UIBarButtonItem.
对于 Xamarin iOS
btnObj.SendActionForControlEvents(UIControlEvent.TouchUpInside);
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
self.btn.sendActions(for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
98385 次 |
| 最近记录: |