[英]how to programmatically fake a touch event to a UIButton?
我正在编写一些单元测试,并且由于这个特定应用程序的性质,我尽可能地在UI
链上处于高位非常重要。 所以,我想做的是以编程方式触发按钮按下,就好像用户按下了GUI
的按钮一样。
(是的,是的——我可以只调用IBAction
选择器,但同样,这个特定应用程序的性质使得我伪造实际按钮按下很重要,这样IBAction
可以从按钮本身调用。)
这样做的首选方法是什么?
事实证明
[buttonObj sendActionsForControlEvents:UIControlEventTouchUpInside];
在这种情况下,我得到了我需要的东西。
编辑:不要忘记在主线程中执行此操作,以获得类似于用户按下的结果。
对于 Swift 3:
buttonObj.sendActions(for: .touchUpInside)
Swift 这个答案的更新
buttonObj.sendActionsForControlEvents(.TouchUpInside)
编辑:为 Swift 3 更新
buttonObj.sendActions(for: .touchUpInside)
斯威夫特 3:
self.btn.sendActions(for: .touchUpInside)
如果你想做这种测试,你会喜欢 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];
}
这里, rightBarButtonItem
是UIBarButtonItem
类型。
这对于编写没有 UI 测试的单元测试的人来说很方便;-)
迅速5的方法来解决它UIBarButtonItem
,其不具有sendAction
方法等的UIButton等
extension UIBarButtonItem {
func sendAction() {
guard let myTarget = target else { return }
guard let myAction = action else { return }
let control: UIControl = UIControl()
control.sendAction(myAction, to: myTarget, for: nil)
}
}
现在您可以简单地:
let action = UIBarButtonItem(title: "title", style: .done, target: self, action: #selector(doSomething))
action.sendAction()
斯威夫特 5:
class ViewController: UIViewController {
@IBOutlet weak var theTextfield: UITextField!
@IBOutlet weak var someButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
theTextfield.text = "Pwd"
someButton.sendActions(for: .touchUpInside)
}
@IBAction func someButtonTap(_ sender: UIButton) {
print("button tapped")
}
}
斯威夫特 4:
self .yourButton(self)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.