简体   繁体   English

如何使用代码模拟Swift iOS8中的按钮按下

[英]How can I simulate a button press in Swift iOS8 using code

How can I simulate a button press within a View in using Swift. 如何使用Swift模拟View中的按钮按下。 I'm trying to get Calendar permissions and have a function setup to check this authorization when I click the start button this currently runs 我正在尝试获取日历权限并设置功能以在我单击当前运行的开始按钮时检查此授权

@IBAction func start(sender: AnyObject) {
//loads prompt to "Allow" or "Don't Allow" calendar permissions
eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: handler)

//checkAuth function looks at calendar authorization to get current status 
and then acts accordingly to dismiss View or load Privacy Settings
checkAuth()

}

I need to be able to simulate a button press of the Start button to trigger the start() func once again. 我需要能够模拟按下“开始”按钮的按钮再次触发start()func。 The only thing I've found so far seems to be in Objective-C 到目前为止,我发现的唯一一件事似乎是在Objective-C中

[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];

I'm not familiar with Obj-C and need a way to implement this in Swift if possible...Thanks 我不熟悉Obj-C,如果可能的话,需要一种在Swift中实现它的方法......谢谢

The Swift translation of: Swift翻译:

[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];

is: 是:

button.sendActions(for: .touchUpInside)

and it should in fact simulate a button push. 它实际上应该模拟按钮按下。

Swift 3 斯威夫特3

button.sendActions(for: UIControlEvents.touchUpInside)

Swift 4 斯威夫特4

button.sendActions(for: .touchUpInside)

尝试调用您的IBAction函数:

self.start(self)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS中模拟按下主屏幕按钮? - How can I simulate a press of the home button in iOS? 如何使用点击手势(iOS8 / Swift)在pageViewController上显示/隐藏状态栏 - How can I show/hide the status bar on a pageViewController using tap gesture (iOS8 / Swift) 如何在iOS 7(Jailbroken)中模拟主页按钮事件? - How to simulate home button press event in iOS 7(Jailbroken)? 如何使用iOS8 Social Framework Swift将视频文件附加到在Facebook上发布 - How can i attach a video file to Publish on Facebook with iOS8 Social Framework Swift 在 iOS8 中使用 Swift 轻按时如何隐藏/显示 tabBar - How do I hide/show tabBar when tapped using Swift in iOS8 Swift:在iOS8中倾斜旋转按钮? - Swift: rotate oblique a button in iOS8? 如何在ios9中按下按钮时打开蓝牙设置? - How can I open bluetooth setting on button press in ios9? iOS8-如何更改iOS 8中长按手势的灵敏度 - iOS8 - How do I change the sensitivity of a long press gesture in iOS 8 如何使用iOS Callkit模拟去电 - How can I simulate an Outgoing call using iOS Callkit 无休止地制作动画循环,直到我在 Swift iOS 中按下按钮。 如何? - Make an animation loop endlessly until I press a button in Swift iOS. How?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM