如何使用Swift模拟View中的按钮按下。我正在尝试获取日历权限并设置功能以在我单击当前运行的开始按钮时检查此授权
@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()
}
我需要能够按下“开始”按钮的按钮来再次触发start()func。我到目前为止唯一发现的似乎是Objective-C
[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];
我不熟悉Obj-C,如果可能的话需要一种在Swift中实现的方法......谢谢
答案 0 :(得分:25)
Swift翻译:
[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];
是:
button.sendActions(for: .touchUpInside)
它实际上应该模拟按钮按下。
答案 1 :(得分:11)
Swift 3
button.sendActions(for: UIControlEvents.touchUpInside)
Swift 4
button.sendActions(for: .touchUpInside)
答案 2 :(得分:1)
尝试调用您的IBAction函数:
self.start(self)